import 'package:flutter/material.dart';
import 'dart:convert';
class Home extends StatefulWidget {
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
Map data = {};
Object? parameters;
@override
Widget build(BuildContext context) {
parameters = ModalRoute.of(context)!.settings.arguments as Map;
Map data = jsonDecode(jsonEncode(parameters));
print(data);
enter code here
return Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
FlatButton.icon(
onPressed: () {
Navigator.pushNamed(context,'/location');
},
icon: Icon(Icons.edit_location),
label: Text('Edit Loaction'),
),
SizedBox(height: 20.0),
Row(
children: <Widget>[
Text(
data['location']
)
],
)
],
),
),
);
}
}
its showing error
type 'Null' is not a subtype of type 'String'.
This error keeps on poping when I am trying to fetch data fro location from the map. Can anyone tell me the solution for this problem.
CodePudding user response:
Getting arguments can be null
parameters = ModalRoute.of(context)?.settings.arguments as Map?;
if (parameters != null) {
data = jsonDecode(jsonEncode(parameters));
/// not sure why you need both `jsonDecode` and `jsonEncode`
}
It is possible to get null while reading map, you can provide default value on null case.
Text(data['location']??"got null")