通过 flutter 中的路由传递数据时检测到类型为 null 的值
Value detected of type null when passing data via routes in flutter
This error just been a while before the data passed successfully from loading screen.
传递数据的加载画面:
if i print the instance on here, the error is not appeared
void setupWorldTime() async {
WorldTime instance = WorldTime(location: 'Jawa Timur', flag: 'jakarta.png', url: 'Asia/Jakarta');
await instance.getTime();
Navigator.pushReplacementNamed(context, '/home', arguments: {
'location': instance.location,
'flag': instance.flag,
'time': instance.time,
});
}
正在接收数据的主屏幕:
Map data = {};
@override
Widget build(BuildContext context) {
data = ModalRoute.of(context)!.settings.arguments as Map;
print(data['location']);
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.only(top: 50),
child: Column(
children: [
TextButton.icon(
onPressed: () {
Navigator.pushNamed(context, '/location');
},
icon: const Icon(Icons.edit_location),
label: const Text('Edit Location'),
),
const SizedBox(
height: 20,
),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Text(
data['time'],
style: const TextStyle(
fontSize: 30,
),
),
]),
],
),
),
),
);
}
打印(数据['location']);完美地打印数据,但在它之前立即显示上面的错误,这是否意味着打印方法在接收值之前期望数据的值为空?如何解决
您的方法 setupWorldTime
是一个异步函数,在 instance.getTime()
返回之前不会调用 Navigator.pushReplacementNamed
位。因为您的主屏幕启动时没有设置参数,data = ModalRoute.of(context)!.settings.arguments as Map
会将数据设置为空,从而导致您的错误。只有在 instance.getTime()
返回后才会再次调用 build()
,这次是 data != null
,您的消息将消失。
要解决此问题,在您的 build
函数中,您应该测试 data == null
并在数据确实仍为空时显示其他内容(如加载指示器),或者使用 FutureBuilder
(首选)。
This error just been a while before the data passed successfully from loading screen.
传递数据的加载画面:
if i print the instance on here, the error is not appeared
void setupWorldTime() async {
WorldTime instance = WorldTime(location: 'Jawa Timur', flag: 'jakarta.png', url: 'Asia/Jakarta');
await instance.getTime();
Navigator.pushReplacementNamed(context, '/home', arguments: {
'location': instance.location,
'flag': instance.flag,
'time': instance.time,
});
}
正在接收数据的主屏幕:
Map data = {};
@override
Widget build(BuildContext context) {
data = ModalRoute.of(context)!.settings.arguments as Map;
print(data['location']);
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.only(top: 50),
child: Column(
children: [
TextButton.icon(
onPressed: () {
Navigator.pushNamed(context, '/location');
},
icon: const Icon(Icons.edit_location),
label: const Text('Edit Location'),
),
const SizedBox(
height: 20,
),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Text(
data['time'],
style: const TextStyle(
fontSize: 30,
),
),
]),
],
),
),
),
);
}
打印(数据['location']);完美地打印数据,但在它之前立即显示上面的错误,这是否意味着打印方法在接收值之前期望数据的值为空?如何解决
您的方法 setupWorldTime
是一个异步函数,在 instance.getTime()
返回之前不会调用 Navigator.pushReplacementNamed
位。因为您的主屏幕启动时没有设置参数,data = ModalRoute.of(context)!.settings.arguments as Map
会将数据设置为空,从而导致您的错误。只有在 instance.getTime()
返回后才会再次调用 build()
,这次是 data != null
,您的消息将消失。
要解决此问题,在您的 build
函数中,您应该测试 data == null
并在数据确实仍为空时显示其他内容(如加载指示器),或者使用 FutureBuilder
(首选)。