我開始用 youtube 指南視頻練習 flutter todo app crud,這是代碼:
![錯誤:無法無條件呼叫方法“[]”,因為接收方可以為“null”。嘗試使呼叫有條件(使用 '?.')](https://img.uj5u.com/2021/12/28/14c050aa2fb049ef99984cf620449f75.png)
但我的代碼將此錯誤寫入 []:
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)
![錯誤:無法無條件呼叫方法“[]”,因為接收方可以為“null”。嘗試使呼叫有條件(使用 '?.')](https://img.uj5u.com/2021/12/28/7ef9278e129d484994235542f6ac8586.png)
我也嘗試過用“!”修復它的建議,但它仍然是錯誤的。
![錯誤:無法無條件呼叫方法“[]”,因為接收方可以為“null”。嘗試使呼叫有條件(使用 '?.')](https://img.uj5u.com/2021/12/28/cddd92590ffe4df08c129964575bd0f1.png)
我也嘗試添加一個“?” 修復它,它仍然是錯誤的
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)
![錯誤:無法無條件呼叫方法“[]”,因為接收方可以為“null”。嘗試使呼叫有條件(使用 '?.')](https://img.uj5u.com/2021/12/28/934a99585a7143fbb8498990578f1e98.png)
uj5u.com熱心網友回復:
我認為問題是因為視頻中使用的是舊版本的 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"];
}
uj5u.com熱心網友回復:
我不知道為什么,但是我就這樣更新了我的依賴項,此時錯誤已經解決了。這么奇怪...
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/395281.html
