Home > OS >  How to save the text filed data automatically in Flutter?
How to save the text filed data automatically in Flutter?

Time:07-26

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.

CodePudding user response:

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.

CodePudding user response:

You can use bloc pattern for this,

You'll have a package for this also: https://pub.dev/packages/flutter_bloc

and you can write your own code also : https://medium.com/codechai/architecting-your-flutter-project-bd04e144a8f1

CodePudding user response:

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