【问题标题】:Getting black screen when doing Navigator.pop. I have tried other links but not getting the solution执行 Navigator.pop 时出现黑屏。我尝试了其他链接但没有得到解决方案
【发布时间】:2020-01-07 07:50:07
【问题描述】:
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


    【解决方案1】:
    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()),
                    );
        });
    

    【讨论】:

    • 没有。我的活动正确重定向但是当我关闭所有其他活动时,最后我得到了黑屏
    【解决方案2】:
     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()));
              }
            });
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-20
      • 2020-10-05
      • 2012-06-06
      • 1970-01-01
      • 2021-03-16
      相关资源
      最近更新 更多