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
2
Answers
As per the description of yours, that value is not showing in
TextFormField
.Value
inTextFormField
is managed through the controller, so just initialize the value ininitstate
as:Try this…