【问题标题】:Flutter Firestore - NoSuchMethodError: The method '[]' was called on nullFlutter Firestore - NoSuchMethodError:在 null 上调用了方法“[]”
【发布时间】:2020-06-07 01:29:20
【问题描述】:

好的。我已经使用 Flutter 一年多了。我几乎在每个应用程序中都使用了相同的代码,并且可以正常工作。出于某种原因,它在我正在构建的这个新应用程序中不起作用。

String testString(DocumentSnapshot doc, String val) {
  try {
    if (doc == null) {
      return "error! DB not found!";
    }
    if (doc[val] == null) {
      return "'" + val + "' doesn't exist in DB";
    }
    return doc[val];
  } catch (e) {
    return "Error: something went wrong";
  }
}

我也试过这个:

String testUndString(DocumentSnapshot doc, String val) {
  try {
    return doc != null ? (doc[val] != null ?  doc[val] : "undefined") :  "undefined";
  } catch (e) {
    return "Error: something went wrong";
  }
}

还有这个:

String testUndString(DocumentSnapshot doc, String val) {
  try {
    return doc.data != null ? (doc[val] != null ?  doc[val] : "undefined") :  "undefined";
  } catch (e) {
    return "Error: something went wrong";
  }
}

经过一番搜索,看起来我已经正确完成了,但它仍然返回错误:

NoSuchMethodError (NoSuchMethodError: The method '[]' was called on null.)

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    试试这个代码:

    if (doc is !DocumentSnapshot) {
        return "error! DB not found!";
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-10
      • 2021-09-29
      • 1970-01-01
      • 2020-04-09
      • 2020-05-23
      • 1970-01-01
      • 2021-11-30
      • 2020-06-07
      • 1970-01-01
      相关资源
      最近更新 更多