我嘗試使用記錄的用戶 ID 從子集合中獲取資料。然后顯示這個錯誤

我試圖從 firestore 子集合中獲取它。集合名稱是“users”,檔案名稱是 userid
代碼
Future<void> getAmount() async {
final id = "${loggedInUser.uid}";
final reference = FirebaseFirestore.instance.doc('users/$id/recharge/$id');
final snapshot = reference.get();
final result = await snapshot.then(
(snap) => snap.data() == null ? null : Amounts.fromJson(snap.data()!));
print('result is ====> $result');
setState(() {
oneAmount = result;
loading = false;
});
}
文本小部件
Text(
oneAmount!.amount,
style: GoogleFonts.roboto(
textStyle: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: kWhite,
),
),
模型
import 'dart:convert';
Amounts articlesFromJson(String str) => Amounts.fromJson(json.decode(str));
String articlesToJson(Amounts data) => json.encode(data.toJson());
class Amounts {
Amounts({
required this.id,
required this.amount,
});
String id;
String amount;
factory Amounts.fromJson(Map<String, dynamic> json) => Amounts(
id: json["id"] ?? "",
amount: json["amount"] ?? "",
);
Map<String, dynamic> toJson() => {
"id": id,
"amount": amount,
};
}
uj5u.com熱心網友回復:
而不是使用!你可以接受空值,如
Text("${oneAmount?.amount}"),
或在 null 情況下提供默認值
Text(oneAmount?.amount?? "got NUll"),
或者做一個空檢查然后顯示小部件
if(oneAmount!=null) Text(oneAmount.amount),
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/526221.html
標籤:Google Cloud Collective 扑镖谷歌云火库飞镖零安全
下一篇:如何在顫振中通過參考傳遞函式?
