简体   繁体   中英

Flutter: Convert firestore snapshot to list in streambuilder

I need to convert a snapshot from cloud firestore to a list, i know this is unnecessary to show the data but i need it to reorder the data based in other parameters, this is my code

 Stream chatRooms;
  List item = [];

 Widget chatRoomsList() {
    return StreamBuilder(
      stream: chatRooms,
      builder: (context, snapshot) {
        if (snapshot.hasData &&
            !snapshot.hasError) {
          item = [];

          item = snapshot.data;

          return ListView.builder(
              itemCount: item.length,
              shrinkWrap: true,
              itemBuilder: (context, index) {
                return ChatRoomsTile(
                  otherUserUid:item[index]['arrayUsers']
                      .replaceAll("[", "")
                      .replaceAll(widget.user.uid, "")
                      .replaceAll("]", "")
                      .replaceAll(",", "")
                      .replaceAll(" ", ""),
                  chatRoomId:
                  item[index]["chatRoomId"],
                  user: widget.user,
                );
              });
        } else
            return Container();
      },
    );
  }

  @override
  void initState() {
    getUserInfogetChats();
    super.initState();
  }

  getUserInfogetChats() async {
    DatabaseMethods().getUserChats(widget.user.uid).then((snapshots) {
      setState(() {
        chatRooms = snapshots;
      });
    });
  }

and im getting this error

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following _TypeError was thrown building StreamBuilder<dynamic>(dirty, state: _StreamBuilderBaseState<dynamic, AsyncSnapshot<dynamic>>#48820):
type 'QuerySnapshot' is not a subtype of type 'List<dynamic>'

Change:

item = snapshot.data;

into this:

item = snapshot.data.documents;

documents should return a List<DocumentSnapshot> , so also change the type of item :

List<DocumentSnapshot> item = [];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM