// ignore_for_file: prefer_const_constructors
import 'package:flutter/material.dart';
import 'package:world_time/pages/loading.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
Map data = {};
@override
Widget build(BuildContext context) {
final data = ModalRoute.of(context)?.settings.arguments;
return Scaffold(
body: SafeArea(
child: Column(
children: [
TextButton.icon(
onPressed: () {
Navigator.pushNamed(context, '/choose_location');
},
label: Text("Edit location"),
icon: Icon(Icons.edit_location_alt_sharp),
),
// ERROR
Text(data['location']), // The method '[]' can't be unconditionally invoked because the
receiver can be 'null'.
Try making the call conditional (using '?.') or adding a
null check to the target ('!')
],
)),
);
}
}
可以从映射键中获取null值,可以使用默认值
Text(data['location']??"default value")
或者在小部件为null时忽略构建它。
if(data['location']!=null)Text(data['location'])
查看有关null safet的更多信息。