【问题标题】:How to retrieve data from firebase firestore into flutter using StreamBuilder and also used ListView.builder to show the values?如何使用 StreamBuilder 将数据从 firebase firestore 检索到颤振中,并使用 ListView.builder 显示值?
【发布时间】:2021-06-05 14:09:49
【问题描述】:

如果我降级版本它可以工作,但我在使用以下版本时遇到问题。

cloud_firestore:^1.0.0

firebase_core: ^1,0.0

和flutter sdk版本:2.0.1

我正在尝试 imaplement 代码。 streamSnapshot.data.length 不起作用

body: StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('chats/UMrLSClddfvaw9wV0hs6/messages')
            .snapshots(),
        builder: (BuildContext context, AsyncSnapshot<dynamic> streamSnapshot) =>
            streamSnapshot.connectionState == ConnectionState.none
                ? Center(
                    child: CircularProgressIndicator(),
                  )
                : ListView.builder(
                    itemCount: streamSnapshot.data,
                    itemBuilder: (context, index) => Container(
                      padding: EdgeInsets.all(8),
                      child: Text('This works'),
                    ),
                  ),
      ),

【问题讨论】:

  • 你遇到了什么问题?
  • itemCount 需要长度。但是我如何获得长度?

标签: firebase flutter google-cloud-firestore flutter-dependencies stream-builder


【解决方案1】:

改变这个:

builder: (BuildContext context, AsyncSnapshot<dynamic> streamSnapshot) =>

进入这个:

builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> streamSnapshot) =>

然后在itemCount你可以这样做:

itemCount: streamSnapshot.data.docs.length

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 2020-12-03
    • 1970-01-01
    • 2021-10-10
    • 2020-10-12
    • 2020-09-10
    相关资源
    最近更新 更多