I have a stream builder in which im calling the peerposts database where there is a favorite field that is type int. I call fields using snapshot.data?.docs[index]['favorite']
function. When I try displaying it in Text Widget as Text(snapshot.data?.docs[index]['favorite']) nothing is displayed so I tried converting this type int field into a to string using Text(snapshot.data?.docs[index]['favorite']).toString()
but I get an error: The argument type 'String?' can't be assigned to the parameter type 'String'. How do I fix this? The rest of the code is written below:
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!');
},
),
],
)
],
),
);
},
),
);
}),
CodePudding user response:
A simple "fix" is:
Text(snapshot.data?.docs[index]['favorite'].toString() ??'MyDefault')