List and ListView builder doesn't update

90 views
Skip to first unread message

Tililweet

unread,
Jun 18, 2020, 4:52:06 PM6/18/20
to Flutter Development (flutter-dev)
So in my code I'm trying to search the database on firebase for an email and return the uid and add it to a list and display it.
List<String> members = [user.uid];
final TextEditingController _searchController = TextEditingController();

return Material(
     child: Container(
       child: AspectRatio(
         aspectRatio: 3 / 2,
         child: Container(
           color: Colors.white,
           child: Column(
             children: [
               groupNameField(_groupNameController),
               groupAvatarField(_groupAvatarController),
               Row(children: [
                 searchField(_searchController, members),
                 IconButton(
                   icon: Icon(Icons.search),
                   onPressed: () async {
                     var s = SearchUserUtil();
                     var searchRes =
                         await s.searchUser(_searchController.text);
                     if (searchRes != null) {
                       setState(() {
                         members.add(searchRes);
                       });
                       _searchController.clear();
                     }
                     //print(await s.searchUser(_searchController.text));
                     else
                       print("User doesnt exists");
                     print(members);
                   },
                 ),
               ]),
               Expanded(
                 child: ListView.builder(
                   itemCount: members.length,
                   itemBuilder: (BuildContext context, int index) {
                     print(index);
                     return Text(members[index]);
                   },
                 ),
               )
             ],
           ),
         ),
       ),
     ),
   );
 }
}
Widget searchField(
    TextEditingController _searchController, List<String> members) {
  return Container(
    width: 500,
    child: TextFormField(
      controller: _searchController,
      decoration: InputDecoration(
        prefixIcon: Icon(
          Icons.email,
          color: Color(0xFF4A41AE),
        ),
        errorText: null,
        focusedBorder: OutlineInputBorder(
          borderSide: BorderSide(
            color: Color(0xFF4A41AE),
          ),
        ),
        hintText: "Search by email",
        labelStyle: TextStyle(
          fontSize: 14,
          color: Color(0xFF4A41AE),
        ),
        border: OutlineInputBorder(
          borderSide: BorderSide(color: Color(0xFF4A41AE)),
        ),
      ),
    ),
  );
}

 So in the members list, the only thing that's in it is the current user's uid(via the first line). Now my problem is that when I search the user, if its found, it adds it to the list but doesn't display it on the list view. I added print statements to try and debug and found that the index doesn't change and it also seems like w/e is added to the list doesn't save. I'm not sure what the problem is but if anyone can help me I'd appreciate it. Thanks!

Hugo van Galen

unread,
Jun 19, 2020, 5:46:44 AM6/19/20
to Tililweet, Flutter Development (flutter-dev)
The code you pasted isn't clear about some things. I wonder what `searchUser` returns? I would assume it could return multiple results in an array because, well, a search can usually return multiple results. If so, the Text() widget won't be able to deal with that. But as you don't mention any errors, I guess it's returning a single text value?

But more importantly (and I guess this is the cause for your problem), where exactly is the `List<String> members` declared? I suspect this is in the `build()` method? If so, then it has the wrong "scope" -- it shall always have a single entry when the build() gets invoked as each call to build() instantiates it with a single element. 

--
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/8f5b4dbd-0a01-47c1-aed4-3686ca3ae522o%40googlegroups.com.

Tililweet

unread,
Jun 19, 2020, 7:56:09 AM6/19/20
to Flutter Development (flutter-dev)
It was the scope issue, thanks :)
To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages