执行Navigator.pop时出现黑屏。我尝试了其他链接,但未获得解决方案

时间:2019-09-04 12:05:18

标签: flutter

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进一步运行该应用程序。

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

我尝试了几种解决方案,但是没有用。

2 个答案:

答案 0 :(得分:3)

 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()));
          }
        });
      }

答案 1 :(得分:0)

If you want your code to execute after the future function has been executed. Then put .then() after the function call. i.e  functionName().then(); 
 _addSharedPreference().then((_){
        var memberId = prefs.getString(SupportableConstant.MEMBERID_Pref);

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