Element added to list in if else but list is empty except if else

17 views
Skip to first unread message

Alper Duzgun

unread,
May 6, 2021, 9:40:29 AM5/6/21
to Flutter Development (flutter-dev)
Hi everyone. I want to if photos liked not added the new list. And I think find the how can i do. If doc exist I can add the list or i can remove the already on user list. But its working only in if else. If I want to read the list except if else method. List is empty. Just working in if else method. How can i find the right solution?

 List<ProfileUsers> usersList = [];
  List<ProfileUsers> likesList = [];
  List<ProfileUsers> _sonucListe = [];

  List<ProfileUsers> get sonucListe => _sonucListe;

  List<ProfileUsers> addUser(ProfileUsers user) {
    likesList.add(user);
    return likesList;
  }

  Future<List<ProfileUsers>> listeOlustur() async {
    /******USERS******/
    var refUser = FirebaseFirestore.instance.collection("users");
    //.where('isActive', isEqualTo: true
    var documentsUser = await refUser.get();
    usersList = documentsUser.docs
        .map((snapshot) => ProfileUsers.fromSnapshot(snapshot))
        .toList();

    usersList.forEach((user) {
      var test = FirebaseFirestore.instance
          .collection('users/2SEHMt5C3POitUCMOj8omnC2PRs1/likes/')
          .doc(user.id);

      test
          .get()
          .then((doc) => {
                if (doc.exists)
                  {
                    //print('???????Document data: ' + doc.id),
                    //usersList.remove(user),
                  }
                else
                  {
                    //print('Böyle bir data yok ' + doc.id),
                    addUser(user),
                    print(sonucListe.length), //OUTPUT : 3
                  },
              })
          .catchError((e) {
        print('ERROR!!! ' + e.toString());
      });
    });
    print(sonucListe.length); //OUTPUT : 0
  }
}

Suzuki Tomohiro

unread,
May 6, 2021, 9:59:00 AM5/6/21
to Alper Duzgun, Flutter Development (flutter-dev)
Because of Future, your code execute  “print(sonucListe.length); //OUTPUT : 0” before items are added to the list.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/5c554cd6-122c-4fe6-811f-1bdd7b79e053n%40googlegroups.com.

Alper Duzgun

unread,
May 6, 2021, 10:12:12 AM5/6/21
to Flutter Development (flutter-dev)
Sorry, I think not understand. Can you make example please?


Adsız.png
6 Mayıs 2021 Perşembe tarihinde saat 16:59:00 UTC+3 itibarıyla suz...@gmail.com şunları yazdı:
Reply all
Reply to author
Forward
0 new messages