Suppose I need to create this Widget():
TextButton(
onPressed: () {},
child: const Text( myModel.getNameOfCustomer() ),
)
Currently, my app queries a EModel class to obtain the value of nameOfCustomer in a synchronic fashion. This is due to the fact, that EModel maintains its storage fully locally.
Suppose, I want to change EModel in such a way, that the class itself queries e.g. a database. Then, getNameOfCustomer() needs to return it data asynchronously and this wrapped inside a Future.
Q: How may I pass the Future<> as the string parameter for Text()
?
Does Text()
has a parameter to pass Future<> futureData
instead of String data
and thus would itself eventually read the value of futureData
and set its String data
?
In other words...
If my app works synchronously regarding the UI and I switch to an asynchronous data model, do I need to rewrite the whole application?
Does a standard pattern exist, which prepares for such a migration?
CodePudding user response:
The thing you are looking for is FutureBuilder. Wrap your Text widget with this, and you'll be able to wait for that Future, and furthermore, while waiting, you can display something else, like a loading indicator/skeleton.