簡體   English   中英

如何將來自快照的數據轉換為列表以適合此列?

[英]how to convert the data coming from snapshot into List to fit in this column?

我的這段代碼向我顯示了 querysnapshot 不是列表的子類型的錯誤。 你能編輯我的代碼並告訴我如何消除這個錯誤。

buildProfilePosts() {
    if (_isLoading) {
      return Center(
        child: Text(
          "Loading...",
          style: TextStyle(fontFamily: "QuickSand"),
        ),
      );
    }
    return StreamBuilder(
      stream: postsRef.document(widget.profileId).collection('userPosts').orderBy('timestamp', descending: true).snapshots(),
      builder: (context, snapshot) {
        return Column(
          children: snapshot.data,
        );
      }
    );
  }

children是小部件Column內的一個屬性,它需要一個小部件列表。 您應該執行以下操作:

child: Column(children: <Widget>[
          StreamBuilder(
            stream: postsRef.document(widget.profileId).collection('userPosts').orderBy('timestamp', descending: true).snapshots(),,
            builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                return ListView.builder(
                    shrinkWrap: true,
                    itemCount: snapshot.data.documents.length,
                    itemBuilder: (BuildContext context, int index) {
                      return ListTile(
                        contentPadding: EdgeInsets.all(8.0),
                        title:
                            Text(snapshot.data.documents[index].data["name"]),
                      );
                    });
              } else if (snapshot.connectionState == ConnectionState.none) {
                return Text("No data");
              }
              return CircularProgressIndicator();
            },
          ),
          ]),

假設您的文檔中有一個name字段。

嘗試

  Column(
          children: <Widget>[
            ...List.generate(
              snapshot.data.length,
                  (index) {
                return Container(
                  child: Text(snapshot.data[index].yourobject),
                );
              },
            ),
          ],
        ),

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM