简体   繁体   中英

how to get int value displayed in text field flutter

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!');
                            },
                            ),
                            ],
                            )
                            ],
                                ),

                            );
                          },
                        ),
                      );
                    }),

一个简单的“修复”是:

Text(snapshot.data?.docs[index]['favorite'].toString() ??'MyDefault')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM