我想在我的应用程序上显示用户设置,但是在将来的构建器上获取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';
final _firestore = Firestore.instance;
最终userRef = _firestore.collection('users');
和其他页面上调用的用户数据模型
如何获取为获取其设置数据而记录的当前用户ID
答案 0 :(得分:2)
您可以在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(),
);
}