Reputation: 165
I'm creating an app which recovers and shows a html text which is stored online on a mysql host. If the snapshot doesn't have any data it should retry the query and rebuild the view until it gets the data.
This is how I'm creating my widget with the data from the database:
FutureBuilder(
future: crud_database.getDatos(url),
builder: (context, snapshot){
if(snapshot.hasError) print(snapshot.error);
if( snapshot.hasData){
return WebView(
initialUrl: UriData.fromString(snapshot.data[0]["webhtml"], mimeType: "text/html").toString() ,
onWebViewCreated: (WebViewController webViewController){
_controller.complete(webViewController);
},
);
}else{
return Center(
child: CircularProgressIndicator(),
);
}
},
)
Upvotes: 4
Views: 2176
Reputation: 10963
You could use StreamBuilder
instead of FutureBuilder
, and create a method that handle the retries and uses the Stream
to notify the UI, something like this:
final _streamController = StreamController<YourData>();
@override
void initState() {
super.initState();
_fetchData();
}
Future _fetchData() async {
YourData data = await crud_database.getDatos(url);
if (data == null) {
_streamController.sink.addError("No data, trying again");
return await _fetchData();
}
_streamController.sink.add(data);
}
@override
void dispose() {
_streamController.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
body: StreamBuilder(
stream: _streamController.stream,
builder: (context, snapshot) {
...
}
)
}
Upvotes: 7