【问题标题】:Flutter | FireStore Error: RangeError (index): Index out of range: index should be less than 1: 1颤振 | FireStore 错误:RangeError(索引):索引超出范围:索引应小于 1:1
【发布时间】:2021-08-23 08:33:16
【问题描述】:

我正在尝试从集合中获取文档列表,但出现超出范围的错误。您可以看到返回列表的函数以及之后的输出。

Future<List> getPreAppliedUserList() async {
    try {
      List? userList = [""];
      QuerySnapshot querySnapshot =
          await _db!.collection(_preAppliedUserCollection).get();
      List allData = querySnapshot.docs.map((doc) => doc.data()).toList();
      print(allData.length); /// 2
      for (int i = 0; i <= allData.length; i++) {
        userList[i] = await allData[i]['customerNumber'];
      }
      return userList;
    } on FirebaseException {
      print("getPreAppliedUserList error");
      var x; ///todo
      return x;
    }
  }

错误:

Error: RangeError (index): Index out of range: index should be less than 1: 1

【问题讨论】:

    标签: javascript firebase flutter dart


    【解决方案1】:

    数组索引为 0,allData.length 的长度为 2。因此您应该使用:

    for (int i = 0; i < allData.length; i++) {
        userList[i] = await allData[i]['customerNumber'];
    }
    

    这意味着删除&lt;= 并将其替换为&lt;,因为您没有索引为2 的项目

    【讨论】:

      猜你喜欢
      • 2021-11-07
      • 1970-01-01
      • 2018-11-06
      • 2017-05-30
      • 2018-12-13
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      相关资源
      最近更新 更多