问题描述
在我切换到 named routes
后,我的应用程序在使用 MaterialPageRoute
时正常运行后出现此错误:
main.dart:
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => ClassA()),ChangeNotifierProvider(
create: (context) => ClassB()),],child: MyApp(),),);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: Firebase.initializeApp(),builder: (context,snapshot) {
if (snapshot.hasError) {
return ...
}
if (snapshot.connectionState == ConnectionState.done) {
return MaterialApp(
debugShowCheckedModeBanner: false,title: 'App Title',initialRoute: '/',routes: {
'/': (context) => Wrapper(),'somePage/': (context) => SomePage(),},);
}
//else ...
return ... ;
});
}
}
错误信息:
The following _CastError was thrown building Builder(dirty):
Null check operator used on a null value
The relevant error-causing widget was:
MaterialApp file:///.../lib/main.dart:40:20
When the exception was thrown,this was the stack:
#0 _WidgetsAppState._onGenerateRoute.<anonymous closure> (package:Flutter/src/widgets/app.dart:1196:48)
#1 MaterialPageRoute.buildContent (package:Flutter/src/material/page.dart:54:55)
#2 MaterialRouteTransitionMixin.buildPage (package:Flutter/src/material/page.dart:107:27)
#3 _ModalScopestate.build.<anonymous closure>.<anonymous closure> (package:Flutter/src/widgets/routes.dart:840:53)
#4 Builder.build (package:Flutter/src/widgets/basic.dart:7555:48)
颤振医生:
Doctor summary (to see all details,run Flutter doctor -v):
[√] Flutter (Channel stable,2.0.2,on Microsoft Windows [Version 10.0.19042.867],locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
[√] Chrome - develop for the web
[√] Android Studio (version 4.1.0)
[√] VS Code (version 1.52.1)
[√] Connected device (3 available)
• No issues found!
奇怪的是,应用程序在多次热重启后工作,然后热重启使其崩溃。
解决方法
我遇到了和你一样的情况,重新启动应用程序解决了这个问题。
,不能同时提供initialRoute和'/',
Route '/' 已经是initialRoute 只需删除initialRoute