Flutter Navigator返回黑屏解决
- 发表于
- flutter
Flutter Navigator黑屏
使用Navigator和静态路由和动态路由从第三级子页面跳到一级页出现黑屏:
child: RaisedButton(
child: Text('走走'),
onPressed: (){
//静态路由
//Navigator.of(context).pushNamed('/twoPage');
//动态路由
Navigator.push(
context,
MaterialPageRoute(builder: (newPage){
return new newTwoPage();
}),
);
}),
在stackoverflow看到一个回复:
This can happen if your
MoviesPage
has anotherMaterialApp
widget. Most of the time you want to useScaffold
as a top widget for a new page/screen, but if you accidentally useMaterialApp
instead, nothing warns you.What happens, is that
MaterialApp
creates a newNavigator
, so if you switch from one page withMaterialApp
to another, you now have two Navigators in the widget tree.The call
Navigator.of(context)
looks for the closest Navigator, so it'll use the one, newly created in yourMoviesPage
. As the history of your route transitions is stored in a first Navigator, this one can't pop back – it has empty route history.Hence, the black screen.
Long story short, to fix this, just use
Scaffold
as a top widget instead ofMaterialApp
in all nested screens.
然后开始检查是否存在第二个MaterialApp
引用,在设置页中发现确实使用了,把MaterialApp去改直接使用Scaffold
问题解决。
一个程序只能有一个MaterialApp
存在,其它都应该使用Scaffold
包含,如果你使用多个MaterialApp
也不会报错,但就会出现类似跳转黑屏这种问题。同时也可能会报如下错误:
flutter: Another exception was thrown: Could not find a generator for route RouteSettings
解决方法也是一样的。
原文连接
的情况下转载,若非则不得使用我方内容。