Flutter / Dart-在Firestore docref.get()。then()外传递/设置变量{??? } [英] Flutter/Dart - Passing/setting variable outside a Firestore docref.get().then() { ??? }

查看:183
本文介绍了Flutter / Dart-在Firestore docref.get()。then()外传递/设置变量{??? }的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Flutter应用程序,它将用管道字符分隔的字符串值转换为ListName(歌曲名称为ListTiles),例如,将歌曲名称(例如: Rudolf | Jingle Bells | White Christmas)转换为ListView ... 。关于我为什么要这样做的长话不说。请耐心等待。我有一个名为集合的Firestore集合,在集合中,我有一个文档,其键为SET01。然后在SET01中,我有一个名为歌曲的字段...一个文本字段,其中包含用管道分隔的歌曲。我调用getSetList()从Firestore检索文本字段。我似乎可以在set01DocRef.get()。then((snapshot)....

I have a simple Flutter application that converts a string value separated by pipe chars...happen to be song names (ie: 'Rudolf|Jingle Bells|White Christmas') into a ListView...with songnames being the ListTiles. Long story as to why I'm doing it this way..please bear with me. I have a Firestore collection called 'sets' and in sets I have a document with a key of SET01. Then in SET01 I have one field called 'songs'...a text field holding songs separated by pipes. I call getSetList() to retrieve the text field from Firestore. I can seem to get the value of 'songs' inside the set01DocRef.get().then((snapshot)....

中获取'歌曲'的值,但是我似乎无法通过值从.then()中返回并返回到函数以将其传递回调用小部件。

But I cannot seem to pass that value out of the .then() and back to the function to pass it back to the calling widget.

非常感谢任何帮助。

ER

String getSetList () {

String songList = 'Loading.....';
DocumentReference set01DocRef = Firestore.instance.collection('sets').document('SET01');

set01DocRef.get().then((snapshot){
  print('String songList inside .then() before assignment:  ' + songList);     //Wagon Wheel|Her Diamonds|Waterfalls
  print('Snapshot value of songs:  ' + snapshot['songs']);         //Wagon Wheel|Her Diamonds|Waterfalls
  songList = snapshot['songs'];     
  print('String songList inside .then() after assignment:  ' + songList);     //Wagon Wheel|Her Diamonds|Waterfalls
});

print('Bueller? Bueller??');
print('String songList outside of .then():  ' + songList);                  

return songList;
}

输出:

flutter: Bueller? Bueller??
flutter: String songList outside of .then():  Loading.....
Reloaded 0 of 424 libraries in 442ms.
flutter: String songList inside .then() before assignment:  Loading.....
flutter: Snapshot value of songs:  Wagon Wheel|Her Diamonds|Waterfalls
flutter: String songList inside .then() after assignment:  Wagon Wheel|Her Diamonds|Waterfalls

**该函数会跳过.get()和立即返回...我知道这可能与强制系统等待该值从FS返回有关...但是我不知道该如何处理。

**The function skips right past the .get() and immediate returns ... I know this probably has something to do with forcing the system to wait for that value to come back from FS...but I don't know how to handle it.

推荐答案

这是带有 then()块的预期行为:一旦加载了数据,该块内的代码便被异步调用,因此要求数据的调用代码必须位于 then()块内。

That's the expected behavior with a then() block: the code inside that block is called asynchronously once the data has been loaded, so call code that requires the data has to be inside of the then() block.

幸运的Dart使用 await 关键字来简化此操作。它基本上替代了 then()块:

Luckily Dart has an await keyword to make this easier. It essentially replaces the then() block:

Future<String> getSetList () async {
  String songList = 'Loading.....';
  DocumentReference set01DocRef = 
  Firestore.instance.collection('sets').document('SET01');

  snapshot = await set01DocRef.get();
  print('String songList inside .then() before assignment:  ' + songList);     
  //Wagon Wheel|Her Diamonds|Waterfalls
  print('Snapshot value of songs:  ' + snapshot['songs']);         //Wagon 
  Wheel|Her Diamonds|Waterfalls
  songList = snapshot['songs'];     
  print('String songList inside .then() after assignment:  ' + songList);     
  //Wagon Wheel|Her Diamonds|Waterfalls

  print('Bueller? Bueller??');
  print('String songList outside of .then():  ' + songList);  
  return songList;
}

请注意,任何对 getSetList()的调用现在还必须使用 await

Note that any calls to getSetList() must now also use await:

var setlist = await getSetList();

这篇关于Flutter / Dart-在Firestore docref.get()。then()外传递/设置变量{??? }的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆