I want the TextFormFied value to persist even when navigating to another page and coming back.
I think it can be achieved by using Provider. but don't know how to update the controller with the proivder class instance variable.
Here is the code I tried.
TextFormField on UI
TextFormField(
amountController: amountController,
onChanged: (value){
provider.amount(value);
},
)),
Provider methode to get user entered value.
String? value ;
amount(String newAmount) {
value = newAmount;
notifyListeners();
}
I don't know whether it's correct or not.
Let me know how to update the amountController
by value
.
If this approach is wrong, Please let me know. What are the possible ways to achieve it?
If you have a stateful widget here, you could set text of your controller inside initState
function like this:
@override
void initState() {
super.initState();
amountController.text = context.read<TestProvider>().value ?? '';
}
so when you get to this page, text field is initialized with the value from provider.