有条件地返回脚手架会引发错误

时间:2018-10-03 04:59:16

标签: dart flutter flutter-layout dart-pub

如果用户已登录,我正在尝试加载新的欢迎支架,这很好,但是在构建欢迎支架之前,错误屏幕会弹出几秒钟,然后显示出来。如果我在某个地方错了,请纠正我。谢谢

This is the error i get before the welcome screen loads up.

WelcomPage:

    class WelcomePage extends StatefulWidget {
    @override
    _WelcomePageState createState() => new _WelcomePageState();
  }      

  enum FormType { admin, member }      

  class _WelcomePageState extends State<WelcomePage> {
    var role;
    SharedPrefClass pref;      

    FormType _formType;
    WidgetsElements widDemo = new WidgetsElements();      

    void moveToadmin() {
      setState(() {
        _formType = FormType.admin;
      });
    }      

    void moveTomember() {
      setState(() {
        _formType = FormType.member;
      });
    }      

    void setFormType() {
      pref = new SharedPrefClass();
      pref.getRoleId().then((value) {
        setState(() {
          this.role = value;
          if (role == 2)
            return moveToadmin();
          else
            return moveTomember();
        });
      });
    }      

    @override
    void initState() {
      // TODO: implement initState
      super.initState();
      setFormType();
    }      

    List<Widget> submitWidgets() {
      switch (_formType) {
        case FormType.admin:
          return [
            MaterialButton(
              minWidth: 200.0,
              height: 42.0,
              onPressed: () {},
              color: Colors.white,
              child: Text('CheckIn/Out', style: TextStyle(color: Colors.grey)),
            ),
            MaterialButton(
              minWidth: 500.0,
              height: 42.0,
              onPressed: () {
                // _login();
              },
              color: Colors.white,
              child: Text('Register POS', style: TextStyle(color: Colors.grey)),
            ),
            MaterialButton(
              minWidth: 200.0,
              height: 42.0,
              onPressed: () {
                // _login();
              },
              color: Colors.white,
              child:
                  Text('Order Placement', style: TextStyle(color: Colors.grey)),
            ),
          ];
        case FormType.member:
          return [
            MaterialButton(
              minWidth: 200.0,
              height: 42.0,
              onPressed: () {
                // _login();
              },
              color: Colors.white,
              child:
                  Text('Register Customer', style: TextStyle(color: Colors.grey)),
            ),
            MaterialButton(
              minWidth: 200.0,
              height: 42.0,
              onPressed: () {
                // _login();
              },
              color: Colors.white,
              child: Text('Customer Order', style: TextStyle(color: Colors.grey)),
            ),
            MaterialButton(
              minWidth: 200.0,
              height: 42.0,
              onPressed: () {
                // _login();
              },
              color: Colors.white,
              child: Text('Re-Order Stock', style: TextStyle(color: Colors.grey)),
            ),
          ];
      }
      return null;
    }      

    checkRole() {
      pref.clearAll().then((_) {
        setState(() {
          this.role = null;
        });
      });
      return role;
    }      

    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(
          title: new Text("Welcome Page"),
          backgroundColor: Colors.redAccent,
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.close),
              onPressed: () {
                print('clicked');
                pref.clearAll();
                print(role);
                if (role == null) Navigator.pushReplacementNamed(context, '/');
              },
            ),
          ],
        ),
        body: new Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new Container(
                  padding: const EdgeInsets.all(16.0),
                  child: new Form(
                      child: new Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: submitWidgets(),
                  ))),
            ],
          ),
        ),
      );
    }
  }

  class LoginPage extends StatefulWidget {
   @override
   _LoginPageState createState() => new _LoginPageState();
 }

 class _LoginPageState extends State<LoginPage> {
   String _appVersion;
   String _deviceId;
   String _platforrm;
   var role;
   bool isLoggedIn = false;

   SharedPrefClass spref = new SharedPrefClass();

   DeveloperManager dmanager = new DeveloperManager();
   String savedeviceid() {
     dmanager.initDeviceId().then((value) {
       setState(() {
         _deviceId = value;
       });
     });
     return _deviceId;
   }

   String saveVersion() {
     dmanager.initPackageInfo().then((value) {
       setState(() {
         _appVersion = value;
       });
     });
     return _appVersion;
   }

   String savePlatform() {
     dmanager.platforms().then((value) {
       setState(() {
         _platforrm = value;
       });
     });
     return _platforrm;
   }

   final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

   RestData restData = new RestData();

   @override
   void initState() {
     // TODO: implement initState
     super.initState();
     savedeviceid();
     saveVersion();
     savePlatform();

     spref.getRoleId().then((updateName) {
       setState(() {
         this.role = updateName;
         if (role != null)
           isLoggedIn = true;
         else
           isLoggedIn = false;

         //  print(role);
       });
     });
   }

   @override
   Widget build(BuildContext context) {

     return !isLoggedIn
         ? Scaffold(
             key: _scaffoldKey,
             appBar: new AppBar(
               title: new Text("Login"),
             ),
             body: Center(
               child: ListView(
                 shrinkWrap: true,
                 padding: EdgeInsets.only(left: 24.0, right: 24.0),
                 children: <Widget>[
                   logo,
                   SizedBox(height: 48.0),
                   email,
                   SizedBox(height: 8.0),
                   password,
                   SizedBox(height: 24.0),
                   loginButton,
                   SizedBox(height: 24.0),
                   // loginButton2,
                 ],
               ),
             ),
           )
         : new WelcomePage();
   }
 }

1 个答案:

答案 0 :(得分:0)

问题可能是由于_formType的值在此处为null

List<Widget> submitWidgets() {
          switch (_formType) {

您可以使用以下方法获得此值:

pref.getRoleId().then((value)...

在使用async之前,您需要等待_formType这个过程完成。但是,您正在_formType内使用变量submitWidgets而不进行任何空检查或检查异步过程是否完成。

一种解决方案是将值waiting添加到enum FormType { waiting, admin, member }

然后初始化_formType = FormType.waiting(以waiting为默认值)

最后处理:

List<Widget> submitWidgets() {
      switch (_formType) {
        case FormType.waiting:
        ...

您可以在此处返回List<Widget>,其中包含加载/请等待消息,例如Text("Loading ...")