首页 > 解决方案 > 执行 Navigator.pop 时出现黑屏。我尝试了其他链接但没有得到解决方案

问题描述

I am getting black screen when hitting Navigator.pop

https://stackoverflow.com/questions/53723294/flutter-navigator-popcontext-returning-a-black-screen/54146583


// Main Code

        void main() => runApp(MaterialApp(

              theme: ThemeData(
                  primaryColor: Color(0xfffea502), accentColor: Colors.blueAccent),
              debugShowCheckedModeBanner: false,
              home: SplashScreen(),
            ));

// 子类

        class SplashScreen extends StatefulWidget {
          @override
          _SplashScreenState createState() => _SplashScreenState();
        }

        class _SplashScreenState extends State<SplashScreen> {

          var prefs = null;

          BuildContext mContext;

          Future<void> _addSharedPreference() async {
            prefs = await SharedPreferences.getInstance();
          }

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



            _addSharedPreference();

            Timer(Duration(seconds: 3), () {
              var memberId = prefs.getString(SupportableConstant.MEMBERID_Pref);


              if (memberId == null || memberId == "0") {

    // Going to other activity

                Navigator.pop(mContext);
                Navigator.push(
                  mContext,
                  MaterialPageRoute(builder: (context) => LoginScreen()),
                );
              } else {

    // Going to other activity

        enter code here
                Navigator.pop(mContext);
                Navigator.push(
                  mContext,
                  MaterialPageRoute(builder: (context) => BottomNavigationScreen()),
                );
              }
            });
          }

这是 main.dart 代码。在这里,我使用 MaterialApp 进一步运行该应用程序。

当我要进行其他活动时,它工作正常,但是如果我从任何活动的上方按下后退按钮,则它会显示黑屏。

我尝试了一些解决方案,但它不起作用。

标签: flutter

解决方案


 class SplashScreen extends StatefulWidget {
      @override
      _SplashScreenState createState() => _SplashScreenState();
    }

    class _SplashScreenState extends State<SplashScreen> {

      var prefs = null;

      BuildContext mContext;

      Future<void> _addSharedPreference() async {
        prefs = await SharedPreferences.getInstance();
      }

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



        _addSharedPreference();

        Timer(Duration(seconds: 3), () {
          var memberId = prefs.getString(SupportableConstant.MEMBERID_Pref);


          if (memberId == null || memberId == "0") {

// Going to other activity


            Navigator.pushReplacement(
              mContext,
              MaterialPageRoute(builder: (context) => LoginScreen()),
            );
          } else {

// Going to other activity



            Navigator.pushReplacement(
              mContext,
              MaterialPageRoute(builder: (context) => BottomNavigationScreen()),
            );

或者

Navigator.of(context).pushReplacement(MaterialPageRoute(
        builder: (BuildContext context) => BottomNavigationScreen()));
          }
        });
      }

推荐阅读