-1

How can I update the UI if I need to wait for the FutureBuilder? Do I need to call my future function twice, one for for the builder and one again to change the UI?

FutureBuilder<String>(
        future: getUserOrder(4045),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Text(snapshot.data,style: Theme.of(context).textTheme.headline);
          } else if (snapshot.hasError) {
            // I need to change the state at this point
            return Text("${snapshot.error}",style: Theme.of(context).textTheme.headline);
          } else {
            return CircularProgressIndicator();
          }
        }),

Calling setState inside the FutureBuilder throws this error:

setState() or markNeedsBuild() called during build.

I don't need to display a button or any other other to be clicked. I want to perform the action automatically when the date is loaded in the futureBuilder

Dani
  • 3,128
  • 2
  • 43
  • 91

1 Answers1

0

Since I couldn't call setState inside FutureBuilder the solution was remove it and do something like this:

getBillingInfo() {
    Provider.of<MyRents>(context, listen: false)
        .getBillingInfo(context)
        .then((billingInfo) {
      setState(() {
        if (billingInfo["companyInfo"] != null &&
            billingInfo["taxes"].isNotEmpty) {
          _canGenerateInvoices = true;
        } else {
          _canGenerateInvoices = false;
        }
      });
    });
  }

...

void initState() {
    super.initState();
    getBillingInfo();
  }

...

Visibility(
 visible: _canGenerateInvoices,
 child: MyWidget()
)

Having this, when I perform other actions I can always change the value of _canGenerateInvoices

Dani
  • 3,128
  • 2
  • 43
  • 91