簡體   English   中英

Null 從 Flutter 中的 Firestore 檢索數據后的列表

[英]Null List after data retrieval from Firestore in Flutter

我是 Flutter 的新手,所以如果這個問題看起來微不足道或無關緊要,我很抱歉!

我創建了另一個 class 用於獲取和設置數據, Repository ,因為我使用 Cloud Firestore,我想要針對這個特定問題的數據存儲在一個集合中,所以我將集合中的所有文檔作為QuerySnapshot然后添加所有List<DocumentSnapshot>中的文檔。

這是方法:

Future<List<DocumentSnapshot>> getCollection(CollectionReference colRef) async {
    List<DocumentSnapshot> dummyList = new List();
    await colRef.getDocuments().then((value) {
      dummyList.addAll(value.documents);
    });
    return dummyList;
  }

存儲庫:

CollectionReference memoriesColRef =
    _firestore
        .collection("username")
        .document("memories")
        .collection("allMem");

    List<DocumentSnapshot> documentList = new List();
    await getCollection(memoriesColRef).then((value) {
      documentList.addAll(value);
    });

畢竟,我已經在我的 UI class 中設置了一個方法來調用這個Repository ,當我在build function 中調用它時,它可以完美地工作,我已經傳遞來訪問數據的全局列表,無法在其中添加值

用戶界面 Class

build(...) {
  getMemories().then((value) {
      print("value size: " + value.length.toString()); // 1
      memoriesListMap.addAll(value); // global variable
    });
  print("valSize: " + memoriesListMap.length.toString()); // 0
  print("val: " + memoriesListMap[0]["title"]); // error line
}

Future<List<Map<String, dynamic>>> getMemories() async{
    List<Map<String, dynamic>> dummyListMap = new List();

    await MemoryOper().getMemories().then((value) {
      print("memVal: " + value[0]["title"]); // appropriate value
      dummyListMap.addAll(value);
    });
    return dummyListMap;
  }

ERROR RangeError (index): Invalid value: Valid value range is empty: 0 \

我不知道是什么原因造成的,但請幫助我! 謝謝

編輯:

ListView.builder(
                itemBuilder: (BuildContext context, int index) {
                  String title = memoriesListMap[index]["title"]; // error prone line
                  int remind = memoriesListMap[index]["remind"];
                  String link = memoriesListMap[index]["link"];

除了 nvoigt 所說的, 本文將幫助您了解如何實現 Future Builder,在您的特定情況下,您可以執行以下操作:

build(...){
..
body: getMemories(),
..
}

Widget getMemories() {
  return FutureBuilder(
    builder: (context, projectSnap) {
      if (projectSnap.connectionState == ConnectionState.none &&
          projectSnap.hasData == null) {
        return Container();
      }
      return ListView.builder(
        itemCount: projectSnap.data.length,
        itemBuilder: (context, index) {
          ProjectModel project = projectSnap.data[index];
          return Column(
            children: <Widget>[
              // Widget to display the list of project
            ],
          );
        },
      );
    },
    future: getCollection(), //this is the important part where you get the data from your Future
  );
}

我認為您在添加元素之前正在訪問它,因為它是async方法。
試試這樣的,

build(...) {
  getMemories().then((value) {
      print("value size: " + value.length.toString()); // 1
      memoriesListMap.addAll(value); // global variable
      print("valSize: " + memoriesListMap.length.toString()); // 0
      print("val: " + memoriesListMap[0]["title"]); 
   });
}

希望它有效!

暫無
暫無

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

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