Home > database >  how to update a text field on a button pressed with a http result?
how to update a text field on a button pressed with a http result?

Time:11-29

The example in flutter doc only shows during initState not onPressed.

I wonder what the correct syntax, to update a text field with a button press that triggers an async http request.

onPressed: () async {
                await http
                    .get(Uri.parse('https://localhost:5001/api/Blog'))
                    .then((value) => setState(() {
                          _httpResult = value.body;
                        }));
              }

CodePudding user response:

If you could add more information on it that would be helpful from what I understand:

You’re trying to update a text field, from a button press that fetched content from an url?

If so then the textfield is filled with a variable so in your StatefulWidget class you Define:

final String yourText = “text blah blah”;

Then in your widget:

Text(“${widget.yourText}”)

And then on the button onPress


Onpress: () async{
   try{
     await [yourhttpcall].then(
    (value) => 
    setState(() {
       widget.yourText = value;});
    );

   } catch(e) {
     print(e);
   }
}

Not sure if this is what you mean? Again with the info you provided, It’s hard to go into specifics.

  • Related