Search code examples
flutterdartgetput

I want to update my DB data from user Input text field


I want to update data by Id, I have userId, name and all details, if I want to change any user details based on user Id so I'm not able to update data and I am getting values above the Text Form Field. please check my update data function and code and help, I am new to flutter.

Future<DataDetails> getDataa() async {
final response = await http.get(
    Uri.parse("get url"));
if(response.statusCode == 200) {
  var temp = DataDetails.fromJson(jsonDecode(response.body));
  return temp;
}
return DataDetails.fromJson(jsonDecode(response.body)) ;
}

 Future<DataDetails> updateData(String cardName,String name,String designation,String 
 company,String address,String email,String phone) async {

var edata = {
  "id": '$id',
  "cardName": cardName,
  "name": name,
  "designation": designation,
  "company": company,
  "address": address,
  "email": email,
  "phone": phone,
};
var ebody = jsonEncode(edata);
final response = await http.put(
    Uri.parse('put url'),
  body: ebody,
  headers: {
    'Content-Type': 'application/json-patch+json',
  }
);
if (response.statusCode == 200) {  
  return DataDetails.fromJson(jsonDecode(response.body));
}
else {
 throw Exception('Failed to update Data.');
}
}

above code is getting value from DB and update.

this code I enter new value for update in DB, but in this code I'm not getting old values inside the Text form Field.

//initState 

 late Future<DataDetails> _futureData;

 @override
 void initState() {
 super.initState();
 _futureData = getDataa();
 }
  
 FutureBuilder(
          future: _futureData,
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              if (snapshot.hasData) {
                return Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text('${snapshot.data!.cardName}'),
                    TextFormField(
                      controller: _cardnameController,
                      decoration: const InputDecoration(
                         hintText: 'Enter Card Name',
                          border: OutlineInputBorder()
                      ),
                    ),
//Update button call:
ElevatedButton(
          onPressed: () {
          setState(() {
              _futureData = updateData(
              _cardnameController.text,
              _nameController.text,
              _roleController.text,
              _companyController.text,
              _addressController.text,
              _phoneController.text,
              _emailController.text,
             );
         });
       },
         child: Text('Update'),
      ),

I'm new to Flutter Please help, thank you


Solution

  • Try this...

    FutureBuilder(
        future: _futureData,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            final data = snapshot.data as DataDetails;
    
            //update the text of controller here
            _controller.text = data.name;
    
            return Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(data.id.toString()),
                TextFormField(
                  controller: _controller,
                  decoration: const InputDecoration(
                         hintText: 'Enter Card Name',
                          border: OutlineInputBorder()
                      ),
                )
              ],
            );
          }
          return const Center(
            child: Text("Loading data..."),
          );
        },
      )