Home > Software design >  how to save TextformField data in flutter
how to save TextformField data in flutter

Time:12-30

I basically have 2 main screens in my app:

  1. main menu - it contains all the pages of my app and i can navigate too those pages from here.
  2. main menu item page - this page contains dynamic multi forms. When i create multiple forms and fill input in textfields and press save i want to save that data of multiple forms so i can use that somewhere else, and when i go back to main menu and come again to menu item page i want all the forms and that data to be there in textfields. my problem is when i go back and come again to menu item page, all mutiforms are lost and all the data is also lost.

enter image description here enter image description here enter image description here

if the form is saved then after navigating to different pages the forms and data should remain on the page.

CodePudding user response:

You can use one of many data storing methods.

  • You can save to database such as mySql
  • You can save to a json file
  • You can use the shared_preferences package
  • Hive database
  • ...

CodePudding user response:

Try firebase database it's a good option for storing your data.

CodePudding user response:

The best way is to locally store the data using shared preference

// Pass your controller.text here
saveToPhone(String name) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    try {
      preferences.setString('name', name);
    } catch (e) {
      print(e.toString());
    }
  }
 
// Get your data while your app opens using this function
  getFromPhone() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    try {
      return preferences.getString('name');
 
    } catch (e) {
      print(e.toString());
    }
  }

Find the complete example of the above here

CodePudding user response:

Create a model class of form, on click event of save

  • you can store value using controller to model class and then access it on another screen OR
  • save data in local database SQLite
  • shared_pref
  • Related