Home > OS >  List not updating in flutter
List not updating in flutter

Time:08-19

I am having trouble updating a list in my flutter app. Basically I want to load a list from a textfile when I load a page, that part works fine. However, when I try to place that list into my global variables, nothing happens. It seems like a really simple problem but I haven't stumbled across something like this in other programming languages like Java and Python.

Here is a simple recreation of the problem

List<String> firstList = [1,2,3,4,5];
globals.secondList = firstList;

If I would now write simple print-debugs as follows:

print(globals.secondList);
print(firstList);

I would get the output: [] [1,2,3,4,5]

It worked earlier when I had all my lists inside the StateFulWidget and used setState() in my setup function. Am I just missing something really basic here, or what is the problem? Thankful for any advice!

Edit: I'm just gonna link the original code

Here are the setups inside my StateFulWidget (inside the class _...State extends State<...>)

 @override
  void initState() {
    super.initState();
    startTimer();
    _setup();
    print(globals.selectedList);
  }

  void filterList(List<String> selectedList){
    selectedList.removeWhere((element) => usedCelebs.contains(element));
  }

  Future<List<String>> _loadQuestions(filePath) async {
    List<String> celebs = [];
    await rootBundle.loadString(filePath).then((names) {
      for (String i in LineSplitter().convert(names)) {
        celebs.add(i);
      }
    });
    return celebs;
  }

  _setup() async {
      // Retrieve the questions (Processed in the background)
      List<String> names_350 = await _loadQuestions('assets/350_people.txt');
      names_350.removeWhere((element) => usedCelebs.contains(element));
      List<String> names_4000 = await _loadQuestions('assets/4000_people.txt');
      globals.people_350 = names_350;
      globals.people_4000 = names_4000;
      // Notify the UI and display the questions
    }

and here is my global library

library globals;
  List<String> people_350 = [];
  List<String> people_4000 = [];
  bool allowPass = false;
  bool allowThieves = false;
  int originalTime = 45;
  int nbrTeams = 2;
  int maxPoints = 16;
  List<String> selectedList = people_350;

CodePudding user response:

A bit tricky to be sure what the problem until you've included some more code.

But something to try is instead:

 globals.secondList = List<String>.from(firstList);

CodePudding user response:

You can just use

globals.secondList.addAll(firstList).

This case will append firstList to secondList.

Or if you not initialize the secondList, you can use factory constructor:

globals.secondList.of(firstList).

This will create a growable list from elements.

To make secondList eaqual to the firstList (thanks to
@Robert Sandberg):

globals.secondList = List.from(firstList);

If you want to store in secondList a reference to firstList, you need to do something like this:

  List<String> firstList = ['1',"2",'3','4','5'];
  var secondList = firstList;
  
  print(secondList.hashCode);
  print(firstList.hashCode);

CodePudding user response:

First ofall you cannot add List<int> values in List<String>

> List<String> firstList = [1,2,3,4,5];=====>List<int> firstList =
> [1,2,3,4,5];
I didn't understand what is global.secondlist
 

     void main() {
        List<int> firstList = [1, 2, 3, 4, 5];
       List<int> secondList = firstList;>     
       print(secondList);
        print(firstList);
     }

Result=====>
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
  • Related