我可以知道我下面的代码有什么问题吗?
我尝试将一些参数从登录屏幕发送到主屏幕, 但我得到的只是错误: 错误:无法在“ModalRoute
所以我的代码设计是,从我的 main.dart 调用 route_generator 来保存所有的路由,main.dart :
import 'package:flutter/material.dart';
import 'package:navigation/constants/app_color.dart';
import 'package:navigation/route_generator.dart';
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
title: 'My Apps',
theme: ThemeData(
appBarTheme: AppBarTheme(
color: Constant.buttonPrimaryColor,
)),
initialRoute: '/loginScreen',
onGenerateRoute: RouteGenerator.generateRoute,
));
}
route_generator.dart 维护所有路由:
import 'package:flutter/material.dart';
import 'package:navigation/screens/login_screen.dart';
import 'package:navigation/screens/home_screen.dart';
import 'package:navigation/screens/about_screen.dart';
class RouteGenerator {
static Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case '/loginScreen':
return MaterialPageRoute (builder: (context) => LoginScreen());
case '/homeScreen':
return MaterialPageRoute (builder: (context) => HomeScreen());
case '/aboutScreen':
return MaterialPageRoute (builder: (context) => AboutScreen());
default:
return _errorRoute();
}
}
static Route<dynamic> _errorRoute() {
return MaterialPageRoute(builder: (context) {
return Scaffold(
appBar: AppBar(
title: Text('ERROR'),
centerTitle: true,
),
body: Center(
child: Text('Page not found!'),
),
);
});
}
}
然后是一些基于 API 响应导航的 services.dart(在此处发送参数),所以基本上我尝试从这个登录屏幕重定向到主屏幕,并且从这个登录屏幕我尝试将一些参数发送到主屏幕并尝试要接收它并将其显示在屏幕上,请在此处放置一些带有姓名和电子邮件的虚拟数据:
if (response.statusCode == 201) {
print('Response status code : ' + response.statusCode.toString());
print('response body : ' + response.body);
Navigator.pushNamed(context, '/homeScreen', arguments: {
'name':'Johny',
'email':'johny@johny.com',
});
} else {
print('Response status code : ' + response.statusCode.toString());
print('exception is : ' + response.body);
throw Exception('Failed to login.');
}
这是 home_screen.dart 从颤振中抛出错误:
import 'package:flutter/material.dart';
import 'package:navigation/constants/app_color.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
final Map<String, dynamic> map = ModalRoute.of(context).settings.arguments;
return WillPopScope(
onWillPop: () => Future.value(false),
child: Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text("Home"),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Welcome to Apps",
style: TextStyle(
fontSize: 20,
),
),
Text(
"Hi, xxx",
style: TextStyle(
fontSize: 20,
),
),
SizedBox(
height: 10,
),
Center(
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, '/aboutScreen');
},
child: Text(
"About",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold
),
),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Constant.buttonPrimaryColor),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
side: BorderSide(color: Constant.buttonPrimaryColor)
)
)
),
),
),
Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text(
"Logout",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold
),
),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Constant.buttonPrimaryColor),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
side: BorderSide(color: Constant.buttonPrimaryColor)
)
)
),
),
),
],
),
),
);
}
}
我尝试遵循一些教程, 在 RouteGenerator 中,我已经通过了 RouteSettings, 试着读了几遍,但仍然困惑, 有人可以解释并向我展示如何传递和显示参数吗?
完整日志:
lib/screens/home_screen.dart:14:59: Error: Property 'settings' cannot be accessed on
'ModalRoute<Object?>?' because it is potentially null.
- 'ModalRoute' is from 'package:flutter/src/widgets/routes.dart' ('../flutter/packages/flutter/lib/src/widgets/routes.dart').
- 'Object' is from 'dart:core'.
Try accessing using ?. instead.
final Map<String, dynamic> map = ModalRoute.of(context).settings.arguments;
^^^^^^^^
lib/screens/home_screen.dart:14:68: Error: A value of type 'Object?' can't be assigned to a variable of type 'Map<String, dynamic>'.
答案 0 :(得分:0)
错误提示意味着一些参数可能为空,所以你应该使用更安全的访问方法。下面的代码可能会帮助你解决这个问题。
final Map<String, dynamic> map = ModalRoute.of(context)?.settings?.arguments;