I want to recreate automatically my page and clear all my TextFields on button press.Is there a method to do it?
Asked
Active
Viewed 875 times
-1
-
You can go over the link below and re-ask your question @SiegfriedMabantey https://stackoverflow.com/help/how-to-ask – void May 16 '20 at 11:08
-
Paste your code! We can see where to fix by using best practises of flutter. The Android implementation is different from Flutter that it store the Intent for the activity and then calls the startActivity by itself so that does not exist in Flutter by advice you paste your code its easy to fix but if you want to force it anyway go here https://stackoverflow.com/questions/50115311/flutter-how-to-force-an-application-restart-in-production-mode – Xenolion May 16 '20 at 11:13
2 Answers
1
I think it's
setState((){
// If there is anything you want to change put it here
});
This will rebuild the widget.

SALAHEDDIN EL HAMRAOUI
- 51
- 1
- 4
-
Share with me your code, I will see where is the problem. – SALAHEDDIN EL HAMRAOUI May 16 '20 at 11:10
1
Edited :
Here is an example of how to achieve this :
class _MyWidgetState extends State<MyWidget> {
final myAController = TextEditingController();
final myBController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: <Widget>[
FlatButton(
onPressed: () {
setState(() {
myAController.clear();
myBController.clear();
});
},
child: Text(
"Clear text fields",
),
),
TextField(
controller: myAController,
decoration: InputDecoration(
border: InputBorder.none, hintText: 'Enter value A'),
),
TextField(
controller: myBController,
decoration: InputDecoration(
border: InputBorder.none, hintText: 'Enter Value B'),
),
],
),
),
);
}
}
As you can see every time i press my flatbutton the widget reloads with the updated counter value.

Firas BENMBAREK
- 306
- 1
- 10
-
It works well.but I don't want to use setState() because I am obligated to clear my TextField manually using this.Isn't there a method in flutter to do it automatically? – Siegfried Mabantey May 16 '20 at 11:57
-
@SiegfriedMabantey i have updated my answer. check if that is what you are looking for. – Firas BENMBAREK May 16 '20 at 17:17
-
Thanks @Firas BENMBAREK.Finally,I used setState() and it works. – Siegfried Mabantey May 16 '20 at 19:23
-
@SiegfriedMabantey You're welcome, please mark the answer as correct if your issue was resolved. – Firas BENMBAREK May 16 '20 at 20:22