Home > Blockchain >  How to store many pass data in flutter
How to store many pass data in flutter

Time:08-25

I have 3 pages as page1 and page2 and page3 ..etc. What I try to do I make page1 as a page of edit post by user so users from this page can edit post like changing the car model and brand..etc. To this point every thing is fine. Now users can move from this page to page2 and page3 in those page there are ListTile and user select car model or brand from those pages and back to page 1 with his choice.

Now my problem if user move to page2 and back with data to page1 I find this data but if then move to page3 and back with new data the data he brought at first time will be deleted and the data just find new data from page3 or last data.

So how can I save the data from loss until the user finishes collecting all the field data he needs without losing it?

Note / the number of pages is just an example. I have more than seven options that the user can modify in the same way above.

This page1 code:

class Page1 extends StatefulWidget {
  var PID;
  var name;
  Page1({Key? key,this.PID,this.name}) : super(key: key);
  @override
  _Page1State createState() => _Page1State();
}

class _Page1State extends State<Page1> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: Scaffold(body: Column(children: [

ElevatedButton(onPressed: (){

  Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => Page2(
       

        ),
      ));

}, child: Text('Move to page2')),

      ElevatedButton(onPressed: (){

        Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => Page3(
              

              ),
            ));

      }, child: Text('Move to page3')),

    ]),),);
  }
}

And this is how user get data to page1 from page2 and page3.

 ListTile( 
  onTap: (){
Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => Page1(
            PID:snapshot.data[index]['PID'],
          
        ),
      ));
}
)

CodePudding user response:

There are of course several ways of doing it. Here are a few:

  1. Have a state management solution using a package such as flutter_bloc to store the state of data from the different pages.
  2. Store the data you receive from the different pages to disk using e.g. the shared_preferences plugin
  3. Pass a Map<int, String> (mapping page number to data) to and from the pages so you on each page always have all the data from all pages

I'd recommend alternative 1.

Edit (to clarify option 3):

Passing data to and from new screen:

    // On FirstScreen
    final result = await Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => NewScreen(data: 'myData'),
      ),
    );

   // On NewScreen
   Navigator.pop(context, 'myData passed back');

result will contain the data to the first screen when you pop from NewScreen (in this case).

Obviously pass the Map<int,String> or whatever data you want instead of the String that I pass in the example above.

Links for reference:

Send data to a new screen

Returning data from a screen

  • Related