[英]read data from firestore flutter
我在 Firestore 中有一組用戶。 我會將此集合中的用戶分數信息打印到屏幕上。 我正在使用 StreamBuilder 執行此操作。 當我在屏幕上打印傳入的值時,這就是返回給我的值。
獲取'_JsonDocumentSnapshot'的分數實例
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return SafeArea(
child: Scaffold(
appBar: buildAppBar(),
body: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('users')
.doc(userID)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const CircularProgressIndicator();
}
var document = snapshot.data;
print("get score ${document??["score"]}");
return Column(
children: <Widget>[
SizedBox(
height: size.height * 0.2,
child: Stack(
children: <Widget>[
Container(
height: size.height * 0.2 - 27,
decoration: BoxDecoration(
color: kPrimaryColor,
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(24),
bottomRight: Radius.circular(24),
)),
child: yourBestScore(),
),
Positioned(
bottom: 0,
left: 0,
right: 0,
child: howToPlay(),
),
],
),
),
const SizedBox(
height: 20,
),
Expanded(
child: Container(
margin: const EdgeInsets.all(8),
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(4)),
child: const UserInformation()),
),
const SizedBox(height: 60),
],
);
},
),
floatingActionButton: floatingActionButton(context),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
),
);
}
??
operation 是一個條件操作,本質上是以下的簡寫:
a != null ? a : b
但這不是您想要的,您需要文檔中的字段,如果沒有文檔,則僅返回null
。 那是用一個單一的?
:
print("get score ${document?["score"]}");
我建議閱讀有關條件表達式的 Dart 文檔和null-aware operator的備忘單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.