簡體   English   中英

Flutter 給 Streambuilder initalData

[英]Flutter give Streambuilder initalData

我有一個Streambuilder將 Firebase Firestore 快照作為 Stream ,我想將initalData添加到Streambuilder

如何將initalData傳遞給Streambuilder

編碼:

StreamBuilder(
  stream: FirebaseFirestore.instance
      .collection("events")
      .snapshots(),
  builder: (BuildContext ctx, AsyncSnapshot<QuerySnapshot> snapshot) {
    if (!snapshot.hasData || snapshot.data.docs.isEmpty) {
      return NoDataRelatedLocation();
    }
    if (snapshot.hasError) {
      return Text(snapshot.error.toString());
    } else {
      return new RelatedLocationListing(
        relatedLocationList: snapshot.data.docs,
      );
    }
  },
),

您可以將initialData添加到StreamBuilder

StreamBuilder(
   initialData: ..... // <~~~ add it here. 
   stream: ... 
   builder: ...

您只需要確保您的initialData與來自 stream 的數據類型相匹配。 由於QuerySnapshot是 Firebase 特定類型,因此您應該將 map 您的 stream 轉換為您可以創建並且您知道的數據類型。

這是一個偽代碼:

initialData: [MyDataType()],
stream: FirebaseFirestore.instance
      .collection("events")
      .snapshots().map((snapshot) => MyDataType.fromMap(snapshot.doc));

暫無
暫無

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

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