Home > Software design >  Flutter Retrieving Data from Firebase
Flutter Retrieving Data from Firebase

Time:06-13

I am writing a social Media app where the user can post and read post from other users. My problem is here, I want an updated name from the user who wrote a post so that the latest name, when the user updates it's name, should appear on the post.

In this code of mine I am getting the error

The argument type 'Future<String?>' can't be assigned to the parameter type 'String'

  Widget postList (){
    return Container(
      child: StreamBuilder(
        stream: some Stream
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot>  snapshot){
          return !snapshot.hasData ?
          CircularProgressIndicator():

          ListView(
            children: snapshot.data!.docs.map((document) {
              return InkResponse(
                child: Container(
                    child: Column(
                      children: <Widget>[
                        Row(
                          children: [

                            // I'm having trouble here
                            /** What I want to happen is to retrieve the NAME of the user using UID so that even if the
                             Writer changes his name, the latest name shall appear on this */

                            Text ( getWriterInformation(uid: document['poster_uid']),
                                style: TextStyle(
                                    fontWeight: FontWeight.bold,
                                    fontSize: 16)),
                          ],
                        ),
                      ],
                    ),
                  ),
              );
            }).toList(),
          );
        },
    );
  }

  Future <String?> getWriterInformation ({required String uid}) async {
    await Users.doc(uid).get().then((value){
      return value['display_name'].cast<String>();
    });
  }


}

If you can recommend any other way aside from my way to do this, I will gladly to appreciate it.

CodePudding user response:

Use FutureBuilder() for Text() widget:

FutureBuilder(
  future: getWriterInformation(uid: document['poster_uid']),
  builder: (BuildContext context, AsyncSnapshot<String> text) {
    return Text(text.data);
  })

This:

  return Container(
  child: StreamBuilder(
    stream: some Stream
    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot>  snapshot){
      return !snapshot.hasData ?
      CircularProgressIndicator():

      ListView(
        children: snapshot.data!.docs.map((document) {
          return InkResponse(
            child: Container(
                child: Column(
                  children: <Widget>[
                    Row(
                      children: [

                        // I'm having trouble here
                        /** What I want to happen is to retrieve the NAME of the user using UID so that even if the
                         Writer changes his name, the latest name shall appear on this */

                        FutureBuilder(
                          future: getWriterInformation(uid: document['poster_uid']),
                          builder: (BuildContext context, AsyncSnapshot<String> text) {
                          return Text(text.data);
                        })
                      ],
                    ),
                  ],
                ),
              ),
          );
        }).toList(),
      );
    },
);

CodePudding user response:

this is happening because getWriterInformation return type of future string. You can't assign future string val into String. Text widget only accepts string. You need futureBuilder for this. some data from the firebase take time so on page launch and show a loader until data comes.

Saitoh Akira answer is correct.

  • Related