简体   繁体   English

如何将来自 Firestore 的用户名添加到 flutter 的应用栏中?

[英]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.

相关问题 我怎样才能只从 Flutter 中的 Firestore 获取我的消息 - How can i get only my messages from firestore in Flutter 每次我使用futurebuilder登录时从firestore中检索用户名并输入appbar标题 - Retrieving username from firestore and put in appbar title everytime I sign in using futurebuilder Flutter/Dart/Firestore:如何将地图列表添加到 firebase? - Flutter/Dart/Firestore: How can I add a list of maps to firebase? 如何在我的 flutter firebase 应用程序中检查用户名可用性 - How can i check username availability in my flutter firebase app Flutter cloud_firestore:如何将集合中的文档添加到DropdownButton中? - Flutter cloud_firestore: How can I add documents from a collection into a DropdownButton? Flutter 和 Firestore:如何从添加 function 中获取文档 ID? - Flutter and Firestore: How can I get the document Id from the add function? 如何在 Flutter 中使用 Firebase firestore 中的数据填充我的 ExpansionTile 小部件 - How Can I fill my ExpansionTile widget with data from Firebase firestore in Flutter Flutter:如何从 Firestore 的 Firebase 存储中获取 URL - Flutter: How Can I Get URL From Firebase Storage In Firestore 我如何从 Firebase FireStore 获取图像到 carouselSlider Flutter - How can i fetch image from Firebase FireStore into carouselSlider Flutter Firestore - 如何在我的 state 中添加文档 ID 及其数据? - Firestore - How can I add the Document ID and their data in my state?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM