【问题标题】:Dart persist data across pagesDart 跨页面持久化数据
【发布时间】:2021-03-25 09:21:59
【问题描述】:

对于一个测验应用程序,我想给各种变量加分。最后,应该显示得分最多的变量。

测验共有 6 页,每个问题都可以用是或否来回答。 我已经尝试为每个StatefulWidget页面设置变量“var teacher = 0;在Flatbutton下++例如变量teacher ++,让变量计数。

这对于第一页效果很好,但在第二页上它又从 0 开始计数。

您对如何最好地编程以及如何全面设置可变页面有什么建议吗?

class Frage1 extends StatefulWidget {
     @override
       _Frage1State createState() => _Frage1State();
     }


class _Frage1State extends State<Frage1> {

    String _ergebnis = "0";

    var teacher = 0;
    var waiter = 0;
    var bus_driver = 0;
    var gardener = 0;

    String Yes_No = '';


    @override
    Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.black,
          shape: Border.all(width: 1 ),
          elevation: 10,
          centerTitle: true,
          title: Text('AMF', style: TextStyle(fontFamily: 'mezzanine.ttf',
              fontWeight: FontWeight.w500,
              fontSize: 65,
              color: Colors.red[700],
              letterSpacing: 15),),
      ),backgroundColor: Colors.black,
            body: Column(children: <Widget>[
              Padding(padding: EdgeInsets.symmetric(horizontal: 10, vertical: 80),),
              Text('1. Do you have fun to work with other people?'
            , style: TextStyle(color: Colors.red[700], fontFamily: 'mezzanine.ttf',fontSize: 38),
                textAlign: TextAlign.center,
            ),
            Row(children: [
              FlatButton(onPressed: (){
                setState(() {
                 
                  teacher++; 
                  waiter++;
                  bus_driver++;

        

                  print(teacher);
                  print(waiter);
                  print(bus_driver);

                });
              debugPrint('Button clicked');

              Navigator.push(context, new MaterialPageRoute(builder: (context) => Frage2()));},

                 child: Text('Yes'
                   ,style: TextStyle(color: Colors.red[700],
                       fontSize: 38.00,
                       fontFamily: 'mezzanine.ttf'
                   ),
                   textAlign: TextAlign.center,
                 ),

              padding: EdgeInsets.symmetric(horizontal: 82, vertical: 90),),
               FlatButton(onPressed: (){
                 setState(() {

                   gardener++;

                   print(gardener);

                 });
              debugPrint('Button clicked');
              Navigator.push(context, new MaterialPageRoute(builder: (context) => Frage2()));},
                child: Text('No'
                  ,style: TextStyle(color: Colors.red[700],
                      fontSize: 38.00,
                      fontFamily: 'mezzanine.ttf'
                  ),
                  textAlign: TextAlign.center,
                ),
                 padding: EdgeInsets.symmetric(horizontal: 40, vertical: 90),),
            ],
            )
            ]
            ),
          )
        );
        }
       @override
       void debugFillProperties(DiagnosticPropertiesBuilder properties) {
        super.debugFillProperties(properties);
        properties.add(DiagnosticsProperty('int', int));
       }
        }

      class Frage2 extends StatefulWidget {
      @override
       _Frage2State createState() => _Frage2State();
        }

       class _Frage2State extends State<Frage2> {

       String _ergebnis = "0";


       var teacher = 0; 
       var waiter = 0;
       var bus_driver = 0;
       var gardener = 0; 

       String Yes_No = ''; 

       @override
       Widget build(BuildContext context) {
        return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.black,
            shape: Border.all(width: 1 ),
            elevation: 10,
            centerTitle: true,
            title: Text('AMF', style: TextStyle(fontFamily: 'mezzanine.ttf',
                fontWeight: FontWeight.w500,
                fontSize: 65,
                color: Colors.red[700],
                letterSpacing: 15),),
          ),backgroundColor: Colors.black,
          body: Column(children: <Widget>[
            Padding(padding: EdgeInsets.symmetric(horizontal: 10, vertical: 90),),
            Text('2. do you like it to drive ?'
              , style: TextStyle(color: Colors.red[700], fontFamily: 'mezzanine.ttf',fontSize: 38),
              textAlign: TextAlign.center,
            ),
            Row(children: [FlatButton(onPressed: (){
              setState(() {
               
              bus_driver++;

              print(bus_driver);

              });
              debugPrint('Button clicked');
              Navigator.push(context, new MaterialPageRoute(builder: (context) => Frage3()));},

              child: Text('Yes'
                ,style: TextStyle(color: Colors.red[700],
                    fontSize: 38.00,
                    fontFamily: 'mezzanine.ttf'
                ),
                textAlign: TextAlign.center,
              ),
              padding: EdgeInsets.symmetric(horizontal: 82, vertical: 90),),
              FlatButton(onPressed: (){
                  setState(() {

                 
                  teacher++;
                  waiter++;
                  gardener++; 

                  print(teacher);
                  print(waiter);
                  print(gardener);
                 
                  });

                debugPrint('Button clicked');
                Navigator.push(context, new MaterialPageRoute(builder: (context) => Frage3()));},
                child: Text('No'
                  ,style: TextStyle(color: Colors.red[700],
                      fontSize: 38.00,
                      fontFamily: 'mezzanine.ttf'
                  ),
                  textAlign: TextAlign.center,
                ),
                padding: EdgeInsets.symmetric(horizontal: 40, vertical: 90),),
            ],
            )
          ]
          ),
        )
    );
  }
}

【问题讨论】:

    标签: flutter dart var statefulwidget


    【解决方案1】:

    当您调用 navigator.push 更改页面时,当前状态被破坏。要跨屏幕保留数据,您可以使用不同的方法:

    • 使用 SQLite
    • 使用共享首选项
    • 使用文件

    我给你这个链接,了解如何做这些事情的更多细节:https://flutter.dev/docs/cookbook/persistence

    【讨论】:

      猜你喜欢
      • 2016-05-27
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多