【问题标题】:Error: The method '[]' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.')错误:无法无条件调用方法“[]”,因为接收者可以为“null”。尝试使呼叫有条件(使用'?.')
【发布时间】:2021-12-27 14:35:51
【问题描述】:

我开始使用 youtube 指南视频练习 Flutter todo app crud,代码如下:

但我的代码将此错误吹入 [ ]:

The method '[]' can't be unconditionally invoked because the receiver can be 'null'.
Try making the call conditional (using '?.') or adding a null check to the target ('!').
dart(unchecked_use_of_nullable_value)

我也尝试了用“!”修复它的建议,但它仍然是错误的。

我也尝试添加一个“?”修复它,它仍然是错误的

The receiver can't be null, so the null-aware operator '?.' is unnecessary.
Try replacing the operator '?.' with '.'.dart(invalid_null_aware_operator)
The getter '[' isn't defined for the type 'DataSnapshot'.
Try importing the library that defines '[', correcting the name to the name of an existing getter, or defining a getter or field named '['.dart(undefined_getter)
Expected an identifier.dart(missing_identifier)

【问题讨论】:

    标签: flutter dart null sdk crud


    【解决方案1】:

    我认为问题在于视频中使用的是旧版本的 Firestore

    尝试改成这样:

    Course. fromSnapShot (DataSnapshot snapshot){
       Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;
    
       _id = snapshot.key;
       name = data["name"];
       description = data["description"l:
       url = data["url"];
       valoration = data["valoration"]:
       imgUrl = data["imgurl"];
    }
    

    【讨论】:

    • 哦,谢谢!这很有用。但在地图中,snapshot.data!吹错误:/没有为“DataSnapshot”类型定义吸气剂“数据”。尝试导入定义“数据”的库,将名称更正为现有 getter 的名称,或定义名为“数据”的 getter 或字段。dart(undefined_getter)
    • 对不起,我以为参数是QuerySnapshot,我猜DataSnapshot是你自己的类,你能显示里面是什么吗?
    【解决方案2】:

    我不知道为什么,但是我就这样更新了我的依赖项,此时错误已解决。好奇怪……

      cloud_firestore: ^3.1.5
      cupertino_icons: ^1.0.2
      firebase_auth: ^1.0.4
      firebase_core: ^1.4.0
      firebase_database: ^6.0.0 
    

    或低于零安全性的依赖

      cloud_firestore: ^0.16.0
      cupertino_icons: ^1.0.2
      firebase_auth: ^0.20.1
      firebase_core: ^1.0.1
      firebase_database: ^6.0.0 
    

    【讨论】:

      猜你喜欢
      • 2021-08-10
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2022-11-17
      • 2021-09-05
      • 2021-10-05
      • 1970-01-01
      • 2022-01-11
      相关资源
      最近更新 更多