0

I have a cupertino picker on flutter that gets its children from firestore.

    return CupertinoPicker(
  itemExtent: 50,
  onSelectedItemChanged: (int index) {
    setState(() {
      _selectedUserId = user[index].userId;
      _selectedUserName = user[index].name;
      IosUserDropdownList.selectedUserId = user[index].userId;
      IosUserDropdownList.selectedUserName = user[index].name;
    });
    print(_selectedUserId);
    print(_selectedUserName);
  },
  children: user.map((user) {
    return CupertinoButton(
      onPressed: () {},
      child: Text(
        user.name,
        style: TextStyle(color: CupertinoColors.activeBlue),
      ),
    );
  }).toList(),
);

This is how I get the users from Firestore:

  Stream<List<UserData>> get users {
    return userCollection.snapshots().map(_userListFromSnapshot);
  }

And convert it to List using my UserData model:

  List<UserData> _userListFromSnapshot(QuerySnapshot snapshot) {
    return snapshot.documents.map((doc) {
      return UserData(
          name: doc.data['name'] ?? '',
          userId: doc.documentID ?? '');
    }).toList();
  }

The problem is that when I navigate to the page with the picker, I dont see any data on the picker. when I do a hot refresh however, the data shows up.

The picker also shows data when I am on the next page and do a Navigator.pop(context) back to the screen with the picker. I have been on this for weeks with no success. Please help.

Thanks.

Sky Lurk
  • 417
  • 1
  • 3
  • 13
  • Generating the list of users directly on the Widget tree is a bit odd. Why are you not generating that elsewhere and keeping your widget stateless? – J. S. Nov 04 '20 at 10:39
  • I am new to this. How would I do that? – Sky Lurk Nov 04 '20 at 14:21
  • How are you getting the children from firestore? I suspect that you're not using await in this case – krumpli Nov 04 '20 at 15:29
  • I get the children using Streams. something like this: Stream> get users { return userCollection.snapshots().map(_userListFromSnapshot); } – Sky Lurk Nov 04 '20 at 15:34

0 Answers0