title: Can't save values when I scroll pages in Flutter
body: <p>I am making a multistep account setup page and ran into an issue, when I scroll to the next page after I have added the data, it erases it on the previous page.
  In other words, I can't save my data.</p>
  <p>I have googled a lot, and know that the solution is in the setting the states and keys, but I am not sure how to implement them in my case.</p>
  <p>main.dart</p>
  <pre><code>import 'package:firebase_core/firebase_core.dart';
  import 'package:firebase_database/firebase_database.dart';
  import 'package:flutter/material.dart';
  import 'package:vibe/AllScreens/LoginPage.dart';
  import 'package:vibe/AllScreens/MainScreen.dart';
  import 'package:vibe/AllScreens/RegistrationScreen.dart';
  import 'package:vibe/AllScreens/RessetPassword.dart';
  import 'package:vibe/AllScreens/UserInfo.dart';
  import 'package:vibe/AllScreens/UserInfo2.dart';
  
  void main() async {
    WidgetsFlutterBinding.ensureInitialized();
    await Firebase.initializeApp();
    runApp(MyApp());
  }
  
  //may be of every class?
  DatabaseReference userRef = FirebaseDatabase.instance.reference().child(&quot;users&quot;);
  //Create second table here for truck drivera data
  class MyApp extends StatelessWidget {
    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
          theme: ThemeData(
            //font family can be changed to a ny font
              fontFamily: &quot;Brand Bold&quot;,
              primarySwatch: Colors.blue,
              visualDensity: VisualDensity.adaptivePlatformDensity
          ),
          //initialRoute: LoginScreen.idScreen,
          home: UserInfo2(),
  
          debugShowCheckedModeBanner: false
      );
    }
  }
  
  </code></pre>
  <p>UserInfo2.dart</p>
  <pre><code>import 'package:dropdownfield/dropdownfield.dart';
  import 'package:flutter/material.dart';
  
  import 'package:vibe/constants.dart';
  import 'package:flutter_swiper/flutter_swiper.dart';
  import 'package:vibe/data.dart';
  
  
  class UserInfo2 extends StatefulWidget {
    static const String idScreen = &quot;userInfo2&quot;;
    @override
    _UserInfo2 createState() =&gt; _UserInfo2();
  }
  SwiperController j=SwiperController();
  class _UserInfo2 extends State&lt;UserInfo2&gt; {
    // var pages=[];
  int tempsex=0;
  var pages=[];
  @override
     void initState(){
       super.initState();
     pages = [
      [Gender(),1],
      [Gender(),1],
      [Gender(),1],
      [Gender(),1],
      [Gender(),1],
      [Gender(),1],
    ];
    }
    final _key = GlobalKey();
    bool enabled=false;
    @override
    Widget build(BuildContext context) {
      var _onPressed = null;
      if (enabled==true){
        _onPressed = (){
          print(&quot;enable&quot;);
        };
      }
      return Scaffold(
          backgroundColor: gradientEndColor,
          body: SingleChildScrollView(
            child: Container(
              decoration: BoxDecoration(
                gradient: LinearGradient(
                    colors: [gradientStartColor, gradientEndColor],
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    stops: [0.3, 0.7]),
  
              ),
  
              child: SafeArea(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: &lt;Widget&gt;[
                    Padding(
                      padding: const EdgeInsets.all(32.0),
                      child: Column(
                        children: &lt;Widget&gt;[
                          Text(
                            'Let us know a bit more about you!',
                            style: TextStyle(
                              fontFamily: 'Avenir',
                              fontSize: 30,
                              color: const Color(0xffffffff),
                              fontWeight: FontWeight.w900,
                            ),
                            textAlign: TextAlign.left,
                          ),
                        ],
                      ),
                    ),
                    Container(
                      height: 600,
  
                      padding: const EdgeInsets.only(left: 16),
                      child: Swiper(
                        loop: false,
  
  
  
  
                        itemCount: 6,//TODO fix this
                        itemWidth: MediaQuery.of(context).size.width-50,
                        itemHeight: 620,
                        layout: SwiperLayout.STACK,
  
                        //dots
                        pagination: SwiperPagination(
                          builder:
                          DotSwiperPaginationBuilder(activeSize: 20, space: 6),//page swiper
  
                        ),
  
                        //height ans stuff can be changed here
                        itemBuilder: (context, index) {
                          return InkWell(
  
                            child: Stack(//Everythink in box
                              children: &lt;Widget&gt;[
                                Column(
                                  children: &lt;Widget&gt;[
                                    Card(
  
  
                                      elevation: 16,
  
                                      shape: RoundedRectangleBorder(
                                        borderRadius: BorderRadius.circular(16),
                                      ),
  
                                      color: Colors.white,
  
                                      child: Padding(
                                        padding: const EdgeInsets.only(top: 16.0, bottom: 16.0),
                                        child: Column(
  
  
                                          crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                          children: &lt;Widget&gt;[
                                            pages[index][0],
  
  
  
  
                                            //SizedBox(height: 40),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
  
                                //NUMBER INDEX
                                Positioned(
                                  right: 10,
                                  bottom: 45,
                                  child: Text(
  
                                    pages[index][1].toString(),
                                    style: TextStyle(
                                      fontFamily: 'Avenir',
                                      fontSize: 160,
                                      color: primaryTextColor.withOpacity(0.08),
                                      fontWeight: FontWeight.w900,
                                    ),
                                    textAlign: TextAlign.left,
                                  ),
                                ),
                              ],
                            ),
                          );
                        },
                        //control: SwiperControl(),
  
                      ),
                    ),
                    Column(
  
                      children: [
                        RaisedButton(
                          color: gradientStartColor,
                          textColor: Colors.white,
                          // focusElevation: 12,
  
                          child: Container(
                            height: 50.0,
  
                            //alignment: Alignment.center,
                            child: Center(
                              child: Text(
                                &quot;Finish!&quot;,
                                style: TextStyle(fontSize: 18.0, fontFamily: &quot;Brand-Regular&quot;),
                              ),
                            ),
                          ),
                          shape: new RoundedRectangleBorder(
                            borderRadius: new BorderRadius.circular(24),
                          ),
                          onPressed: _onPressed,
  
                        ),
                      ],
                    )
                  ],
                ),
              ),
  
            ),
          )
      );
    }
  
  
  }
  
  //===============================================
  
  
  
  //===============================================================
  
  
  class Gender extends StatefulWidget {
    //const Gender({Key key}) : super(key: key);
    //List&lt;int&gt; _selectedItems = List&lt;int&gt;();
  
  
  
    @override
    _GenderState createState() =&gt; _GenderState();
  }
  
  class _GenderState extends State&lt;Gender&gt; {
  
  
  
    @override
    void initState() {
      _selectedItems.add(gender);
      selected=true;
      print(gender);
  
    }
    List&lt;String&gt; genders=[&quot;Male&quot;,&quot;Female&quot;,&quot;Transgender Male&quot;, &quot;Transgender Female&quot;, &quot;Other&quot;];
    int gender;
    List&lt;int&gt; _selectedItems = List&lt;int&gt;();
    bool selected=false;
  
    @override
    Widget build(BuildContext context) {
      return Container(
        height: 500,
        child: Container(
          child: Column(
            children: [
              Text(
                &quot;Gender&quot;,
                style: TextStyle(
                  fontFamily: 'Avenir',
                  fontSize: 30,
                  color: Colors.black54,
                  fontWeight: FontWeight.w900,
                ),
              ),
              SizedBox(height: 8,),
              Text(
                &quot;choose one&quot;,
                style: TextStyle(
                  fontFamily: 'Avenir',
                  fontSize: 20,
                  color: Colors.black54,
                  fontWeight: FontWeight.normal,
                ),
  
              ),
              SizedBox(height: 8,),
              SizedBox(
                height: 400,
                child: ListView.builder(
  
  
                  itemCount: genders.length,
                  itemBuilder: (context, index){
                    return
                      Padding(
                        padding: const EdgeInsets.all(4.0),
                        child: Container(
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(16),
                              color: (_selectedItems.contains(index)) ? Colors.greenAccent.withOpacity(0.5) : Colors.transparent,
                              // border: Border.all(color: (_selectedItems.contains(index)) ? gradientStartColor.withOpacity(0.5) : Colors.transparent,),
                              border: Border.all(color: gradientStartColor.withOpacity(0.5) ),
                              boxShadow: [(_selectedItems.contains(index)) ? BoxShadow(
                                color: Colors.grey.withOpacity(0.5),
                                spreadRadius: 2,
                                blurRadius: 7,
                                offset: Offset(0, 3), // changes position of shadow
                              ):BoxShadow(color: Colors.transparent)
                              ]
  
                          ),
                          child: ListTile(
  
  
                              onTap: (){
                                setState(() {
                                  if(selected &amp;&amp; _selectedItems.contains(index)) {
                                    _selectedItems.removeWhere((val) =&gt;
                                    val == index);
                                    selected = false;
                                    gender=null;
  
                                  }else if(!selected &amp;&amp; !_selectedItems.contains(index)){
                                    gender=index;
  
                                    _selectedItems.add(gender);
                                    selected=true;
  
                                  }
                                  else{
                                    _selectedItems.removeWhere((val) =&gt;
                                    val == _selectedItems[0]);
                                    gender=index;
                                    _selectedItems.add(gender);
                                  }
  
                                });
                                print(gender);
  
                              },
                              title:Text( genders[index].toString(),
                                textAlign: TextAlign.center,)
                          ),
                        ),
                      );
                  },
                ),
              ),
            ],
          ),
  
        ),
      );
    }
  
  }
  
  Widget smth(BuildContext context){
    return Container(
      child: Text(
        &quot;huy&quot;
      ),
    );
  }
  
  
  </code></pre>