[英]How can i add username from firestore into my appbar in flutter?
So i have been trying to make an app in flutter, but i cannot seem to succeed in getting the username from the firestore database into a variable Here is my code!!所以我一直在尝试在 flutter 中制作一个应用程序,但我似乎无法成功地将用户名从 firestore 数据库中获取到变量中这是我的代码! EDIT:
编辑:
final FirebaseAuth _auth = FirebaseAuth.instance;
Future<String> inputData() async {
String _username = '';
final userCol = Firestore.instance.collection("users");
final FirebaseUser user = await _auth.currentUser();
final uid = user.uid;
// here you write the codes to input the data into firestorenull
final data = await userCol.document(uid).get().then(
(value) {
final username = value.data["username"];
_username = username;
},
);
return _username;
}
I have tried it in many many different ways, as async, as future, with FutureBuilder etc but i cannot seem to find the solution.我已经以许多不同的方式尝试了它,如异步、未来、FutureBuilder 等,但我似乎找不到解决方案。 Here is the appbar:
这是应用栏:
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
return Stack(
children: <Widget>[
Scaffold(
appBar: appbar.ProfileAppBar(name: _username)
............................................................
I think we need more information (such as whether or not you are successfully retrieving the username and it is getting properly set), but it looks like you aren't setting the state of the screen after getting the username.我认为我们需要更多信息(例如您是否成功检索用户名并且设置正确),但看起来您在获取用户名后没有设置屏幕的 state。
Can you try你能试一下吗
setState(() {
final username = docs.data["username"];
this._username = username;
});
Your function also appears to be incorrect.您的 function 似乎也不正确。 You are defining username as an empty string, and then defining an asynchronous function to get the username, and immediately returning username (the empty string).
您将用户名定义为空字符串,然后定义异步 function 以获取用户名,并立即返回用户名(空字符串)。 You will need to use a class variable and inside of the setstate, set the class variable as what you get from your asyncronous '.then()' function
您将需要使用 class 变量并在 setstate 内部,将 class 变量设置为您从异步 '.then()' function 获得的变量
So what i in fact did was to create a FutureBuilder, which helped a lot and i recommend it to everyone!!!所以我实际上做的是创建一个 FutureBuilder,它有很大帮助,我推荐给大家!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.