简体   繁体   English

Flutter / Dart-调用未来的函数 <String> …但只需要返回一个字符串

[英]Flutter/Dart - calling a function that is a Future<String> … but needs to return only a String

I have an async function that is calling out to Firestore to pull in a data value. 我有一个异步函数正在调出Firestore来提取数据值。 I got a lot of help in a previous post...learned a lot...and wanted to start over with hopefully a cleaner question. 在上一篇文章中,我得到了很多帮助……学到了很多东西……并希望从一个更清洁的问题开始。 So I have the following function 所以我有以下功能

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;
}

This function logic works...I can print() out the songList var (string var) to the console and I see the value from Firestore. 该函数逻辑起作用...我可以将songList var(string var)打印()到控制台,然后从Firestore中看到值。 When I try to call the function: 当我尝试调用该函数时:

@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),
      ),

That setList variable is not a String. 该setList变量不是String。 When I print it [print(setList.toString()] it shows as an instance of Future String. 当我打印它[print(setList.toString()]时,它显示为Future String的一个实例。

I tried using: var setList = await getSetList(); 我尝试使用: var setList = await getSetList(); but that shows an error on the await. 但这显示了等待中的错误。

Any ideas appreciated. 任何想法表示赞赏。

When do you need to call the future? 您什么时候需要打电话给未来?

You can always create a tmp variable and try to load it. 您始终可以创建一个tmp变量并尝试加载它。 You cannot randomly put futures into the build process. 您不能将期货随机放入构建过程中。 You need to grab the data then call setState to notify the widget if the view has changed. 如果视图已更改,则需要获取数据,然后调用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.

I hope my set state does not have any sytax errors. 我希望我的设置状态没有任何语法错误。

https://docs.flutter.io/flutter/widgets/State/setState.html https://docs.flutter.io/flutter/widgets/State/setState.html

https://docs.flutter.io/flutter/widgets/State/initState.html https://docs.flutter.io/flutter/widgets/State/initState.html

you can't use await in a function that is not async, which means the use 您不能在非异步函数中使用await,这意味着

var setList = await getSetList();

in your build function is wrong. 在您的构建函数中是错误的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM