Flutter/Dart-调用作为Future< String>的函数...但是只需要返回一个字符串 [英] Flutter/Dart - calling a function that is a Future<String> ... but needs to return only a String

查看:744
本文介绍了Flutter/Dart-调用作为Future< String>的函数...但是只需要返回一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个异步函数,该函数调用Firestore提取数据值.在上一篇文章中,我得到了很多帮助……学到了很多东西……并希望从一个更清洁的问题开始.所以我有以下功能

I have an async function that is calling out to Firestore to pull in a data value. 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;
}

此函数逻辑起作用...我可以将songList var(字符串var)打印()到控制台,然后从Firestore中看到值.当我尝试调用该函数时:

This function logic works...I can print() out the songList var (string var) to the console and I see the value from 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),
      ),

该setList变量不是字符串.当我打印它[print(setList.toString()]时,它显示为Future String的一个实例.

That setList variable is not a String. When I print it [print(setList.toString()] it shows as an instance of Future String.

我尝试使用:var setList = await getSetList();,但是在等待时显示错误.

I tried using: var setList = await getSetList(); but that shows an error on the await.

任何想法表示赞赏.

推荐答案

您什么时候需要打电话给未来?

When do you need to call the future?

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

You can always create a tmp variable and try to load it. 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.

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/initState.html

这篇关于Flutter/Dart-调用作为Future&lt; String&gt;的函数...但是只需要返回一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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