首页 > 解决方案 > 如何在futurebuilder中设置用户ID

问题描述

我想在我的应用程序上显示用户设置,但在未来的构建器上获取 currentuserid 时遇到问题,

当我直接从 firestore 插入 id 时,它工作正常,但是当我设置“widget.userId”时,我得到一个错误“该方法在 null 上被调用”

这是我在设置页面上的未来构建器代码

      body:  FutureBuilder(
    future: userRef.document(widget.userId).get(),
    builder: (BuildContext context, AsyncSnapshot snapshot){
      if(!snapshot.hasData){
        return Center(
          child: new CircularProgressIndicator(valueColor: new AlwaysStoppedAnimation<Color>(Colors.white),strokeWidth: 3.0,),);
      }
      User user = User.fromDoc(snapshot.data);
      return ListView(
        children: <Widget>[
          Column(
            children: <Widget>[
              SizedBox(height: 50.0,),
              SizedBox(height: 50.0,),
              Center(
                child: CircleAvatar(
                  radius: 50,
                  backgroundImage: AssetImage('assets/images/default-profile.jpg'),
                ),
              ),
              SizedBox(height: 10.0,),
              Center(
                child: Text(
                  user.username,
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: 18.0,
                      fontWeight: FontWeight.bold
                  ),
                ),
              ),
              SizedBox(height: 50.0,),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Column(
                    children: <Widget>[
                      Text(
                        '12',
                        style: TextStyle(
                            fontSize: 18.0,
                            fontWeight: FontWeight.w600,
                            color: Colors.white
                        )
                        ,),
                      Text(
                        'playlists',
                        style: TextStyle(
                            color: Colors.white60
                        ),
                      )
                    ],
                  ),
                  Column(
                    children: <Widget>[
                      Text(
                        'username',
                        style: TextStyle(
                            fontSize: 18.0,
                            fontWeight: FontWeight.w600,
                            color: Colors.white
                        )
                        ,),
                      Text(
                        'followers',
                        style: TextStyle(
                            color: Colors.white60
                        ),
                      )
                    ],
                  ),
                  Column(
                    children: <Widget>[
                      Text(
                        '123',
                        style: TextStyle(
                            fontSize: 18.0,
                            fontWeight: FontWeight.w600,
                            color: Colors.white
                        )
                        ,),
                      Text(
                        'following',
                        style: TextStyle(
                            color: Colors.white60
                        ),
                      )
                    ],
                  ),
                ],
              ),
              SizedBox(height: 15.0,),
              Center(
                child:  Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    new Container(
                      width: 200.0,
                      child: FlatButton(
                        onPressed: (){

                          Navigator.push(
                              context,
                              MaterialPageRoute(builder: (context) => Edit_profile()));

                        },
                        color: Colors.green,
                        textColor: Colors.white,
                        child: Text(
                          'Edit Profile',
                          style: TextStyle(fontSize: 18.0),
                        ),
                      ),
                    ),
                    new Container(
                      width: 60.0,
                      child: OutlineButton(
                        onPressed: (){

                        },

                        borderSide: BorderSide(color: Colors.white),

                        shape: RoundedRectangleBorder(
                          side: BorderSide(width: 1.0, style: BorderStyle.solid, ),
                          borderRadius: BorderRadius.all(Radius.circular(8.0)),
                        ),

                        color: Colors.transparent,
                        child: Center(
                          child: new Icon(EvaIcons.bookmark,color: Colors.white,),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Divider(),
            ],

          ),
        ],

      );
    },

  ),

'userRef.document' 被引用到另一个页面

import 'package:cloud_firestore/cloud_firestore.dart';

最终_firestore = Firestore.instance;

最终 userRef = _firestore.collection('users');

和从另一个页面调用的用户数据模型

如何获取为检索其设置数据而记录的当前用户 ID

标签: authenticationfluttergoogle-cloud-firestore

解决方案


您可以在 initState() 中的任何页面/路由中获取用户详细信息,如下所示。

String uid="";

    @override
          void initState() {
            super.initState();

              FirebaseAuth.instance.currentUser().then((user) {
                setState(() {
                  uid = user.uid;
                  displayName = user.displayName;
                  email = user.email;
                  photoUrl = user.photoUrl;
                });
              });
          }

这样您就可以找到所有用户详细信息。

此外,在您的情况下,您使用的是需要 uid 的 FutureBuilder。因此,您可以处理您的构建方法,例如,

@override
  Widget build(BuildContext context) {
  return Scaffold(
        body: uid==null && uid.length==0 ?
              Container() : 
              Your_Future_Builder_Widget_Here(),
  );
}

推荐阅读