authentication - 如何在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
解决方案
您可以在 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(),
);
}
推荐阅读
- java - 如何以编程方式从 karaf bundle 获取 log4j2 配置
- firebase - 颤振/firebase项目中cocopods缺少兼容版本
- xamarin - Xamarin 表单拒绝访问路径
- javascript - 呈现包含对象的列表对象的最有效方法
- machine-learning - 在 LSTM 的输入门中使用 tanh 激活函数
- shopify - 如何使用 Api 在 shopify 的店面中显示自定义应用程序表单?
- python - 出现错误:AttributeError: 'list' object has no attribute 'keys',但是所谓的 'list' 是字典,我该怎么办?
- python - ModuleNotFoundError: 没有命名的模块 (projectname.appname)
- r - 在数据框中创建一列,在列的行内随机分布两个级别
- python - 用 shapefile 屏蔽 nc 文件并将其保存为 nc 格式