0

i want to fetch data from api and set it as text field initial value, then after user changes it i will post it to another api, but after changing textfield value, future builder rebuilds and textfield gets back to initial value, here is my code:

services.dart

Future<Data> getdata() async {
  final response = await http
      .get(url);
  if (response.statusCode == 200) {
    print(response.body);
    final parsed = jsonDecode(response.body);
    var data = Data.fromMap(parsed);
    return data;
  } else {
    throw Exception('failed to load profile Details');
  }
}

homePage.dart

TextEditingController Controller = TExtEDitingController;
late Future<Data> futureData;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    futureData = getdata();
  }
.
.
.
FutureBuilder<Details>(
   builder: (context, snapshot) {
      if (snapshot.hasData) {
       Controller.text= snapshot.data;

return TextField(
          controller: Controller,
          .
          .
          .   
                    ),      
dinomim
  • 21
  • 5
  • how you've include the future, can you include that part on `FutureBuilder` – Md. Yeasin Sheikh Aug 02 '22 at 13:12
  • Exactly what I was going to ask, to know whether or not to post this FAQ: Don't build the future as the future: parameter in FutureBuilder. See the first few paragraphs of the FutureBuilder documentation, or see my video at https://www.youtube.com/watch?v=sqE-J8YJnpg. – Randal Schwartz Aug 02 '22 at 16:04
  • I would suggest that instead of using a future builder, you use a simple TextField and call this getdata() method in the initState of the page and then use the other method with the post API in the onChange or onFieldSubmitted of the TextField. – Waqad Arshad Aug 07 '22 at 23:55

1 Answers1

0

I would suggest that instead of using a future builder, you use a simple TextField and call this getdata() method in the initState of the page and then use the other method with the post API in the onChange or onFieldSubmitted of the TextField

Waqad Arshad
  • 329
  • 1
  • 9