简体   繁体   English

字符串名称不会改变 dart flutter

[英]string name dose not change dart flutter

im assigning the string name in this code to a name from a document but it dose not change from null i cant see why.我将此代码中的字符串名称分配给文档中的名称,但它不会从 null 改变我不明白为什么。

i will use this variable inside a listTile我将在 listTile 中使用这个变量

String name='';
    FirebaseFirestore.instance
        .collection('Users')
        .doc('list_students')
        .collection('Students')
      ..where('Email', isEqualTo: userEmail)
          .get()
          .then((QuerySnapshot querySnapshot) {
        querySnapshot.docs.forEach((doc) {
          name = doc['Full Name'];
        });
      }); 

As commenters have said, most of your code is running asynchronously, because the data needs to be loaded from the cloud.正如评论者所说,您的大部分代码都是异步运行的,因为需要从云端加载数据。 This messes up the order in which your code runs, and is likely why the variable doesn't have a value.这会打乱代码运行的顺序,这可能是变量没有值的原因。

Some logging should make this clearer:一些日志记录应该使这一点更清楚:

String name='';
print("1. Before starting query");
FirebaseFirestore.instance
    .collection('Users')
    .doc('list_students')
    .collection('Students')
  ..where('Email', isEqualTo: userEmail)
      .get()
      .then((QuerySnapshot querySnapshot) {
    print("2. Got data");
    querySnapshot.docs.forEach((doc) {
      name = doc['Full Name'];
    });
  }); 
print("3. After starting query");

When you run this code, the output will be:当您运行此代码时,output 将是:

  1. Before starting query开始查询前

  2. After starting query开始查询后

  3. Got data得到数据

This is probably not the order you expected, but is actually working as intended.这可能不是您预期的顺序,但实际上按预期工作。 And this means that if you'd print the name in the place of 3. you'd not see the value from the database.这意味着如果您将name打印在3.的位置,您将看不到数据库中的值。


The solution for this is always the same: the code that needs the data from the database needs to be inside the then callback, or it needs to be called from there, or it needs to be otherwise synchronized to run when the data has loaded.解决方案总是相同的:需要来自数据库的数据的代码需要在then回调中,或者需要从那里调用,或者需要在加载数据时同步运行。

For example, if you want to use the name in the UI of your app, you can store it in the state of your (stateful) widget by wrapping it like this:例如,如果您想在应用程序的 UI 中使用该name ,您可以将其存储在(有状态)小部件的 state 中,如下所示:

setState(() {
  name = doc['Full Name'];
})

Now Flutter knows that it needs to update the UI after the data has loaded.现在 Flutter 知道它需要在数据加载后更新 UI。

Since this is such a common occurrence, there is a dedicated widget to handling it known as a FutureBuilder .由于这种情况很常见,因此有一个专用的小部件来处理它,称为FutureBuilder So I recommend reading up on that too.所以我建议也阅读一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义名称“用户”flutter dart - Undefined name 'users' flutter dart 排序 Map<string, dynamic> 在 flutter dart 其中第一个键是字段名称</string,> - Sort Map<String, dynamic> in flutter dart where the first key is the field name 无法转换 Future<String> 字符串并在 Dart (Flutter) 中使用 - Cannot convert Future<String> to String and use in Dart (Flutter) Flutter/Dart 返回 '_Future 的实例<string> ' 而不是实际的字符串值</string> - Flutter/Dart returning Instance of '_Future<String>' instead the actual String Value 如何从flutter dart中firebasestorage中该文件的链接获取文件名 - How to get name of a file from the link of that file in firebasestorage in flutter dart flutter web 实时数据库不起作用 - flutter web realtime database dose not work 队列在飞镖/颤振中 - Queue's in dart/flutter 从 Firebase 中的 JSON 字符串解码为 Map 时出现问题<string, model>在颤振/飞镖</string,> - Having problems in decoding from a JSON String in Firebase to a Map<String, Model> in Flutter/Dart 将 android 工作室的 google-service.json 更改为 firebase_option.dart 项目项目 Z5ACEBC4CB70DDBBAEZB0AC76AAB176 - Change google-service.json from android studio to firebase_option.dart on flutter project 如何将 Dart 快照 object 数据从 Flutter Firestore 转换为字符串变量? - How to Convert Dart Snapshot object data to String Variables from Flutter Firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM