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
}
}
}
),
],
);
}
Perhaps I'm not following the "mantra" of Flutter? I appreciate your consideration and input on this.