flutter - 执行 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 进一步运行该应用程序。
当我要进行其他活动时,它工作正常,但是如果我从任何活动的上方按下后退按钮,则它会显示黑屏。
我尝试了一些解决方案,但它不起作用。
解决方案
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()));
}
});
}
推荐阅读
- c# - ASP.NET Core 2.0 URL
- java - springboot2.0 undertow1.4.x 使用 %D 处理请求的打印时间配置属性“server.undertow.accesslog.pattern”失败
- php - Opencart注册去掉必填字段后不注册用户(不返回错误)
- html - 在 github repo 中查看 html 文件?
- ios - 删除使用 NSMutableAttributedString 的 UILabel 空白区域的彩色背景
- r - How do I create a vector of characters from a character object?
- react-native - 如何在 React Native 中显示不同的选定项目
- .net - 如何使用 ExcelDataReader 库获取列宽?
- python - Pandas Dataframe - 在特定行中选择具有特定值的列
- javascript - React Native:记录音频板输出的库