【问题标题】:Flutter: Convert firestore snapshot to list in streambuilderFlutter:将firestore快照转换为streambuilder中的列表
【发布时间】:2020-10-26 21:06:35
【问题描述】:

我需要将快照从云存储库转换为列表,我知道这对于显示数据是不必要的,但我需要它根据其他参数重新排序数据,这是我的代码

 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;
      });
    });
  }

我得到了这个错误

════════ 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>'

【问题讨论】:

  • 你在哪一行得到错误
  • @PeterHaddad 在这一行:item = snapshot.data;

标签: list firebase flutter dart google-cloud-firestore


【解决方案1】:

变化:

item = snapshot.data;

进入这个:

item = snapshot.data.documents;

documents 应该返回一个List&lt;DocumentSnapshot&gt;,所以还要更改item 的类型:

List<DocumentSnapshot> item = [];

【讨论】:

    猜你喜欢
    • 2019-07-23
    • 2020-06-16
    • 2020-07-19
    • 2019-06-14
    • 2021-07-01
    • 2023-02-11
    • 2019-03-26
    • 2020-10-01
    • 1970-01-01
    相关资源
    最近更新 更多