我有一個流構建器,我在其中呼叫 peerposts 資料庫,其中有一個最喜歡的欄位型別為 int。snapshot.data?.docs[index]['favorite'] 我使用函式呼叫欄位。當我嘗試在 Text Widget 中將其顯示為 Text(snapshot.data?.docs[index]['favorite']) 時,沒有顯示任何內容,因此我嘗試使用將這種型別的 int 欄位轉換為字串, Text(snapshot.data?.docs[index]['favorite']).toString() 但出現錯誤:引數輸入“字串?” 不能分配給引數型別“字串”。我該如何解決?其余的代碼寫在下面:
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('peerPosts').snapshots(),
builder: (context,snapshot){
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return SingleChildScrollView(
child: ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount:snapshot.data?.docs.length,
itemBuilder: (context,index){
return ListTile(
subtitle: Column(
children: [
Row(
children: [
IconButton(
icon: new Icon (Icons.thumb_up_alt_rounded), onPressed: () {
setState(() {
final DocumentReference docRef = FirebaseFirestore.instance.collection("peerPosts").doc(snapshot.data?.docs[index]['postId']);
docRef.update({"favorite": FieldValue.increment(1)});
});
},),
Text(snapshot.data?.docs[index]['favorite'] ),
IconButton(
icon: new Icon(Icons.message),
onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => commentSection() ));
},
),
IconButton(
icon: new Icon(Icons.share),
onPressed: (){
Share.share('https://www.webpagetest.org/', subject: 'You should check this out!');
},
),
],
)
],
),
);
},
),
);
}),
uj5u.com熱心網友回復:
一個簡單的“修復”是:
Text(snapshot.data?.docs[index]['favorite'].toString() ??'MyDefault')
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/486757.html
