Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter add item to list

I would like to add an item to a list:

void submitAll() async {
List<UserSearchItem> userSearchItems = [];
Firestore.instance
    .collection('insta_users')
    .snapshots()
    .listen((data) =>
    data.documents.forEach((doc){
      print(data.documents.length);

      User user = new User.fromDocument(doc);
      UserSearchItem searchItem = new UserSearchItem(user);
      userSearchItems.add(searchItem);
      print(user.bio);

    }));
print("Loaded");
print(userSearchItems.length);
}

But if I print the length of the list to the console, it always says, the list is 0 long...

print(userSearchItems.length);

Any suggegstions?

Best Regards

like image 333
Martin Seubert Avatar asked Mar 28 '19 14:03

Martin Seubert


People also ask

How do you add items to your Dart list?

Using addAll() method to add all the elements of another list to the existing list. Creating a new list by adding two or more lists using addAll() method of the list. Creating a new list by adding two or more list using expand() method of the list.

How do you add multiple values to a list in flutter?

The List. addAll() function accepts multiple values separated by a comma and appends these to the List.

How do you add a string to a list in darts?

In Dart, we can use the '+' operator to concatenate the strings. Example: Using '+' operator to concatenate strings in Dart.


1 Answers

I will try to give an explanation of what is happing here take a look on this code:

import 'dart:async';

void main() {
  List<int> userSearchItems = [];

  Timer _sendTimeOutTimer;

  const oneSec = Duration(seconds: 2);
  _sendTimeOutTimer = Timer.periodic(oneSec, (Timer t) {
    userSearchItems.add(1);
    print(userSearchItems.length); // result 1 and it will be executed after 2 seconds 
    _sendTimeOutTimer.cancel();
  });

  print(userSearchItems.length); // result 0 and it will be executed first
}

The print inside asynchronous action(Timer) it will be executed after 2 seconds means after the asynchronous action ends but the one which is outside of asynchronous action(Timer) it will be executed directly without waiting 2 seconds, in your case the asynchronous action is listening to data .listen((data) =>, so if you print the length outside of your asynchronous action you will not see the deferent because the item is not added yet.

Solution: you can create function witch return Future and then wait until it's finished then print the length.

List<UserSearchItem> userSearchItems = [];

Future<String> submitAll() async {

Firestore.instance
    .collection('insta_users')
    .snapshots()
    .listen((data) =>
    data.documents.forEach((doc){
      print(data.documents.length);

      User user = new User.fromDocument(doc);
      UserSearchItem searchItem = new UserSearchItem(user);
      userSearchItems.add(searchItem);
      print(user.bio);

      return 'success';
    }));
}

void yourFunction() async{
   await submitAll();
   print("Loaded");
   print(userSearchItems.length);
}

Then call yourFunction().

like image 74
Taym95 Avatar answered Sep 21 '22 09:09

Taym95