Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I "retry" a build?

Tags:

flutter

dart

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(),
            );
          }


        },
      )
like image 256
Landy Cuadra Avatar asked Oct 25 '25 12:10

Landy Cuadra


1 Answers

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) {
        ...
      }
    )
  }
like image 145
Pablo Barrera Avatar answered Oct 27 '25 00:10

Pablo Barrera