0

I've this code in a Flutter page.

This code is supposed to show to the user that his orders had been send to the database.

I use a future with the function .then and I should get the response status code 200 which is printed in the debug console of my app.

Does someone know why my _textInfo String variable doesn't take the future as new value ?

Container(
                        padding: const EdgeInsets.all(Pages.edgeInsetsAll),
                        child: ElevatedButton(
                            onPressed: () {
                              setState(() {
                                for (int i = 0; i < myb.length; i++) {
                                  if (myb[i].count > 1) {
                                    for (int j = 0; j < myb[i].count; j++) {
                                      postOrder(
                                          widget.basketId,
                                          Session.sessionQRCode,
                                          "Pas Membre",
                                          "Especes",
                                          Session.sessionEstaName,
                                          myb[i].price,
                                          myb[i].product,
                                          myb[i].add).then((value) {
                                            _textInfo = 'ORDER : $value';
                                      });
                                    }
                                  } else {
                                    postOrder(
                                        widget.basketId,
                                        Session.sessionQRCode,
                                        "Pas Membre",
                                        "Especes",
                                        Session.sessionEstaName,
                                        myb[i].price,
                                        myb[i].product,
                                        myb[i].add).then((value){
                                          _textInfo= 'ORDER : $value';
                                    });
                                  }
                                }
                                myb.clear();
                              });
                            },
                            child: Text(_textInfo)),
                      ),
LeGros
  • 19
  • 5
  • can you try with converting method to async like `onPressed: () async{` and then also `setState(() async {});`. It would be nice if you can provide [minimal-reproducible-example](https://stackoverflow.com/help/minimal-reproducible-example) – Md. Yeasin Sheikh Jun 20 '22 at 14:48
  • This is the answer of the debug service : Error: setState() callback argument returned a Future. The setState() method on _LocalBasketListState#6bccf was called with a closure or method that returned a Future. Maybe it is marked as "async". Instead of performing asynchronous work inside a call to setState(), first execute the work (without updating the widget state), and then synchronously update the state inside a call to setState(). – LeGros Jun 20 '22 at 15:02
  • what happen if you just use `onPressed: () async{` – Md. Yeasin Sheikh Jun 20 '22 at 15:02
  • The app throws an error meaning that I can't use a set state into a future; I've to use the set state method after getting the future value... but with the MYFUTURE.then() function I can't do otherwise than calling the setState into the then method... I don't understand how to fix this – LeGros Jun 20 '22 at 15:10
  • Can try try removing `setState` wrapper and calling it at the end after else bloc. – Md. Yeasin Sheikh Jun 20 '22 at 15:12
  • ok now I get the setState refresh after getting the 200 status code, but the _textInfo variable still doesn't change... I guess I should do without this variable change ... Thank you very much for your Help @Yeasin – LeGros Jun 20 '22 at 15:20
  • you are welcome, are you using it inside a dialog?if dialog, try wrapping with statefulBuilder – Md. Yeasin Sheikh Jun 20 '22 at 15:22
  • no I'm using it inside a responsive grid list which is used inside a future builder – LeGros Jun 20 '22 at 15:29
  • ah, if `_textInfo` directly depends on future, you can try calling future inside initState, by separating another future method. – Md. Yeasin Sheikh Jun 20 '22 at 15:33
  • Ok I'll try it, I didn't knwo that Flutter allowed devs to call 2 differents Future in the same widget – LeGros Jun 20 '22 at 15:36

0 Answers0