Flutter 从 Firebase 检索数据

安杰洛

我正在编写一个社交媒体应用程序,用户可以在其中发布和阅读其他用户的帖子。我的问题在这里,我想从写帖子的用户那里得到一个更新的名字,这样当用户更新它的名字时,最新的名字应该出现在帖子上。

在我的这段代码中,我收到了错误

参数类型“Future<String?>”不能分配给参数类型“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>();
    });
  }


}

如果您可以推荐除我的方式之外的任何其他方式,我将很高兴地感激它。

斋藤明

用于小FutureBuilder()部件Text()

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

这个:

  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(),
      );
    },
);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章