英文:
Null Error in Flutter Web: MaterialApp causing NullPointerException on start
问题
我在我的Flutter Web应用程序中遇到了一个与MaterialApp小部件相关的空值错误。当我运行应用程序时,立即收到一个指向第30行的NullPointerException,该行是MaterialApp小部件开始的地方。自从我添加了路由部分以来,我一直遇到这个错误。
import 'package:flutter/material.dart';
// 其他导入
void main() {
runApp(RateMyPicApp());
}
class RateMyPicApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'RateMe',
theme: ThemeData(
// 主题配置
),
initialRoute: '/upload',
onGenerateRoute: (settings) {
switch (settings.name) {
case '/rate':
final Map<String, dynamic> args = settings.arguments as Map<String, dynamic>;
if (args != null && args.containsKey('id')) {
final String id = args['id'];
return MaterialPageRoute(builder: (context) => RatingPage(imageId: id));
}
break;
case '/upload':
default:
return MaterialPageRoute(builder: (context) => UploadScreen());
}
},
// 其他属性和配置
);
}
}
// 其他类和代码
英文:
I'm encountering a null error in my Flutter web app, specifically related to the MaterialApp widget. When I run the app, I immediately receive a NullPointerException pointing to line 30 where the MaterialApp widget starts. I am experiencing the error since i have added the routing part.
import 'package:flutter/material.dart';
// Other imports
void main() {
runApp(RateMyPicApp());
}
class RateMyPicApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'RateMe',
theme: ThemeData(
// Theme configuration
),
initialRoute: '/upload',
onGenerateRoute: (settings) {
switch (settings.name) {
case '/rate':
final Map<String, dynamic> args = settings.arguments as Map<String, dynamic>;
if (args != null && args.containsKey('id')) {
final String id = args['id'];
return MaterialPageRoute(builder: (context) => RatingPage(imageId: id));
}
break;
case '/upload':
default:
return MaterialPageRoute(builder: (context) => UploadScreen());
}
},
// Other properties and configuration
);
}
}
// Other classes and code
答案1
得分: 1
尝试这个解决方案:只需在末尾添加null
:
import 'package:flutter/material.dart';
import 'package:user_interface_1/temp1.dart';
import 'package:user_interface_1/temp2.dart';
void main() {
runApp(RateMyPicApp());
}
class RateMyPicApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'RateMe',
theme: ThemeData(
// 主题配置
),
initialRoute: '/upload',
onGenerateRoute: (settings) {
switch (settings.name) {
case '/rate':
final Map<String, dynamic> args =
settings.arguments as Map<String, dynamic>;
if (args != null && args.containsKey('id')) {
final String id = args['id'];
return MaterialPageRoute(builder: (context) => RatingPage());
}
break;
case '/upload':
default:
return MaterialPageRoute(builder: (context) => UploadScreen());
}
return null;
},
);
}
}
这是你提供的代码的中文翻译部分。
英文:
Try this Solution: Just add null
at the end:
import 'package:flutter/material.dart';
import 'package:user_interface_1/temp1.dart';
import 'package:user_interface_1/temp2.dart';
void main() {
runApp(RateMyPicApp());
}
class RateMyPicApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'RateMe',
theme: ThemeData(
// Theme configuration
),
initialRoute: '/upload',
onGenerateRoute: (settings) {
switch (settings.name) {
case '/rate':
final Map<String, dynamic> args =
settings.arguments as Map<String, dynamic>;
if (args != null && args.containsKey('id')) {
final String id = args['id'];
return MaterialPageRoute(builder: (context) => RatingPage());
}
break;
case '/upload':
default:
return MaterialPageRoute(builder: (context) => UploadScreen());
}
return null;
},
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论