How to save the text filed data automatically in Flutter?

Viewed 37

There are two text fields in one screen. After giving the inputs to text filed, with out clicking the done button if you go back to the previous screen and come back to this text filed screen the text got erased!!

can we get this? Please let me know if its possible.

Thanks in advance.

3 Answers

You can always use any state management tool for instance if you are using React or React Native you can use redux to store the field values and then when you mount the component where your fields are you check if the properties you have in the store have some value then you show those values in the text field.

Declare your variable as globally as in the below example or use sharedprefrence. If you don't want both you can go for state management BLOC, PROVIDER.

   String s = "";
   class LoginScreen extends StatefulWidget {
      @override
      _LoginScreen createState() => _LoginScreen();
    }
    
   class _LoginScreen extends State<LoginScreen> {
     
      @override
      void initState() {    
        super.initState();
      }
    
      @override
      Widget build(BuildConteTextFormField(
                controller: TextEditingController(
                    text: s),           
         );
      }
    }
Related