Flutter-是否可以在不使用FutureBuilder的情况下从Future中提取数据? [英] Flutter - Is it possible to extract data from a Future without using a FutureBuilder?

查看:36
本文介绍了Flutter-是否可以在不使用FutureBuilder的情况下从Future中提取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 TextField 中读取用户提供的输入(在本例中为邮政编码),我需要针对数据库进行有效性检查.但是,我需要在提交按钮(在这种情况下为 RaisedButton ) onPressed:(){} lambda函数内部进行异步数据库查询.在大多数编程语言中,这是一个相当直接和简单的任务.但是,我在Flutter中遇到的问题是,从异步数据库查询返回的 Future 对象只能由 FutureBuilder 对象使用,而该对象又仅返回 Widget 对象.我只需要返回一个 String ,然后就可以使用它通过 MaterialPageRoute 对象传递到新路线,或者在不更改路线的情况下向用户显示错误.Flutter有什么办法做到这一点?返回小部件对我来说毫无用处,因为我不想创建一个新的小部件来显示.我正在使用Flutter 0.3.2和Dart 2.0.0

I'm reading in user-provided input (in this case a zip code) from a TextField that I need to check against a database for validity. However, I need to make an asynchronous database query inside of the submit button's (a RaisedButton in this case) onPressed: () {} lambda function. In most programming languages, this is a fairly straightforward and simple task. The problem I'm running into in Flutter, however, is the fact that Future objects returned from asynchronous database queries can only be consumed by FutureBuilder objects which in turn only return Widget objects. I simply need a String returned that I can then use to either pass to a new route via a MaterialPageRoute object, or display an error to the user without changing routes. Is there any way to do this with Flutter? Returning a Widget is useless to me as I don't want to create a new Widget for display. I am using Flutter 0.3.2 and Dart 2.0.0

作为我需要在何处调用数据库查询的简化示例:

As a simplified example of where I need to call the database query:

@override
Widget build(Buildcontext context) {
    return new Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
            new Container(
                padding: const EdgeInsets.all(16.0),
                child: new TextField(
                    keyboardType: TextInputType.number,
                    controller: _controller,
                    decoration: new InputDecoration(
                    hintText: 'Zip Code',
                ),
                onSubmitted: (string) {
                  return string;
                },
              ),
            ),
            new RaisedButton(
                onPressed: () {
                        // use regex to test against user input
                        if (_controller.text != null && _controller.text.isNotEmpty) {
                            RegExp zipCodeRegExp = new RegExp(r"^(\d{5})$");

                            // if the user input validates...
                            if (zipCodeRegExp.hasMatch(_controller.text)) {
                            zipCode = _controller.text;

                           // need to perform database query here and return a string, not a Widget

                            } else {
                               // an else condition here
                            }
                        } else {
                           // an else condition here
                        }
                    }
                }
            ),
        ],
    );
}

也许我没有遵循Flutter的口头禅"?感谢您的考虑和投入.

Perhaps I'm not following the "mantra" of Flutter? I appreciate your consideration and input on this.

推荐答案

此后我就已经弄清楚了(我相信这是Günter最初所说的,但是当时我不清楚的根本原因).无需创建 Widget 对象即可使用 Future 的唯一方法是使用 Future API. Future API允许解析 Future 对象,就好像它是 AsyncSnapshot 对象一样(这是解析 .data的地方). FutureBuilder builder:函数中).这可以在返回的 Future 对象(可以将 async await 结合使用)上执行.例如:

I've since figured this out (I believe this is what Günter was originally saying, but the fundamental reason why wasn't clear to me at the time). The only way to be able to consume a Future without creating a Widget object is by using the Future API. The Future API allows the parsing of a Future object as though it was an AsyncSnapshot object (which is where one would parse .data in a FutureBuilder builder: function). This can be performed on a returned Future object (which can use async with await). For example:

Future regionName = dbClient.getRegionNameFromZipCode(int.parse(zipCode)); <-- this database method getRegionNameFromZipCode returns a Future object and uses async and await

regionName.then((data) {
   String hZonesString = data[0]['hzone'];
   print(hZonesString);
}, onError: (e) {
     print(e);
   });

一旦您了解如何利用 Future API,并且与使用 FutureBuilder 相比,它的意图就相当简单.很高兴认识这种语言的新手,例如我自己!

This is rather simple once you understand how the Future API can be leveraged, and it's intent vs. using FutureBuilder. Good to know for newbies of this language such as myself!

这篇关于Flutter-是否可以在不使用FutureBuilder的情况下从Future中提取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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