Asynchronous 颤振/飞镖-调用未来的函数<;字符串>。。。但是只需要返回一个字符串

Asynchronous 颤振/飞镖-调用未来的函数<;字符串>。。。但是只需要返回一个字符串,asynchronous,dart,flutter,google-cloud-firestore,future,Asynchronous,Dart,Flutter,Google Cloud Firestore,Future,我有一个异步函数,调用Firestore以获取数据值。我在以前的帖子中得到了很多帮助…学到了很多…希望能从一个更清晰的问题开始。所以我有下面的函数 Future<String> getSetList () async { DocumentReference set01DocRef = Firestore.instance.collection('sets').document('SET01'); var snapshot = await set01DocRef.get(); s

我有一个异步函数,调用Firestore以获取数据值。我在以前的帖子中得到了很多帮助…学到了很多…希望能从一个更清晰的问题开始。所以我有下面的函数

Future<String> getSetList () async {

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

var snapshot = await set01DocRef.get();

songList = snapshot['songs']; //works, get expected text value from FS

return songList;
}
Future getSetList()异步{
DocumentReference set01DocRef=Firestore.instance.collection('sets')。document('SET01');
var snapshot=wait set01DocRef.get();
songList=snapshot['songs'];//工作正常,从FS获取预期的文本值
返回歌曲列表;
}
这个函数逻辑起作用了…我可以打印()出歌曲列表变量(字符串变量)到控制台,然后我可以从Firestore中看到值。当我尝试调用该函数时:

@override
  Widget build(BuildContext context) {

    var setList = getSetList();

    print('In widget:  ' + setList.toString()); //shows as instance of Future<String>

    //List<String> items = setList.split('|');
    List<String> items = ['Red','White','Blue'];

    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
@覆盖
小部件构建(构建上下文){
var setList=getSetList();
print('In widget:'+setList.toString());//显示为Future的实例
//List items=setList.split(“|”);
列表项=[‘红色’、‘白色’、‘蓝色’];
归还新脚手架(
appBar:新的appBar(
标题:新文本(widget.title),
),
该setList变量不是字符串。当我打印它[print(setList.toString()]时,它显示为未来字符串的实例

我尝试使用:
var setList=await getSetList();
,但这表明await上有一个错误


任何想法都值得赞赏。

您不能在非异步函数中使用wait,这意味着

var setList = await getSetList();

你的构建函数是错误的。

你什么时候需要调用未来

您始终可以创建一个tmp变量并尝试加载它。您不能将未来随机放入构建过程。您需要获取数据,然后调用setState以通知小部件视图是否已更改

String _setList = null;
//initState called when the widget is mounted.
void initState() {
    super.initState();
    if(_setList == null){
       getSetList().then(
          (String s) => setState(() {_setList = s;})
       );
    }
}

@override
  Widget build(BuildContext context) {

    String setList = _setList;

    print('In widget:  ' + setList.toString()); //shows as instance of Future<String>
    if(setList != null){
    //List<String> items = setList.split('|');
    List<String> items = ['Red','White','Blue'];

    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
    } else { return const CircularProgressIndicator();  }
    //Create a progress circle.
String\u setList=null;
//挂载小部件时调用initState。
void initState(){
super.initState();
如果(_setList==null){
getSetList()。然后(
(字符串s)=>setState((){u setList=s;})
);
}
}
@凌驾
小部件构建(构建上下文){
字符串集合列表=_集合列表;
print('In widget:'+setList.toString());//显示为Future的实例
if(setList!=null){
//List items=setList.split(“|”);
列表项=[‘红色’、‘白色’、‘蓝色’];
归还新脚手架(
appBar:新的appBar(
标题:新文本(widget.title),
),
}else{return const CircularProgressIndicator();}
//创建一个进度循环。
我希望我的设置状态没有任何sytax错误


如中的注释所述,当调用
async
方法时,您需要使用
wait
来获取值(而不是
Future
值)。任何使用
wait
的方法本身都必须标记为
async
。感谢您的持续帮助…很抱歉,我仍然没有“理解”。我将继续阅读并尝试。我不敢相信我的语法正确。我从未运行过它。我编写了一个简单的示例。我唯一需要调整的是“const setList…”需要是“字符串设置列表…”非常感谢。这是一个很好的例子@user1462442。谢谢。在Flatter 2.0.5中需要进行一些小的调整(主要是可空性问题)。字符串_setList=“”;(不能将空值分配给字符串类型)。