I am creating a multi-paged app on flutter to To determine the time based on the location, but when the code is turned on, there's a problem on this page, I tried to fix the problem, but the error still exists" type 'Null' is not a subtype of type 'bool'" I'll include the other pages in the comments.
import 'package:flutter/material.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
Map? data={};
@override
Widget build(BuildContext context) {
if (data!= null) {
data = ModalRoute.of(context)!.settings.arguments as Map?;}
// set background image
final String? bgImage = data?['isDayTime'] ? 'day.png' : 'night.png';
final Color? bgColor = data?['isDayTime'] ? Colors.blue : Colors.indigo[700];
return Scaffold(
backgroundColor: bgColor,
body: SafeArea(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('asset/$bgImage'),
fit: BoxFit.cover,
)
),
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 120.0, 0, 0),
child: Column(
children: <Widget>[
FlatButton.icon(
onPressed: () async {
dynamic result = await Navigator.pushNamed(context, '/location');
if(result != null){
setState(() {
data = {
'Time': result['Time'],
'Location': result['Location'],
'isDayTime': result['isDayTime'],
'Flag': result['Flag']
};
});
}
},
icon: Icon(
Icons.edit_location,
color: Colors.grey[300],
),
label: Text(
'Edit Location',
style: TextStyle(
color: Colors.grey[300],
),
),
),
SizedBox(height: 20.0),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
data?['Location'],
style: TextStyle(
fontSize: 28.0,
letterSpacing: 2.0,
color: Colors.white,
),
),
],
),
SizedBox(height: 20.0),
Text(
data?['Time'],
style: TextStyle(
fontSize: 66.0,
color: Colors.white
)
),
],
),
),
),
),
);
}
}
CodePudding user response:
Instead of
final String? bgImage = data?['isDayTime'] ? 'day.png' : 'night.png';
final Color? bgColor = data?['isDayTime'] ? Colors.blue : Colors.indigo[700];
write
final String? bgImage = (data?['isDayTime'] ?? false) ? 'day.png' : 'night.png';
final Color? bgColor = (data?['isDayTime'] ?? false) ? Colors.blue : Colors.indigo[700];
and also instead of
setState(() {
data = {
'Time': result['Time'],
'Location': result['Location'],
'isDayTime': result['isDayTime'],
'Flag': result['Flag']
};
});
write
setState(() {
data = {
'Time': result['Time'],
'Location': result['Location'],
'isDayTime': result['isDayTime'] ?? false,
'Flag': result['Flag']
};
});
CodePudding user response:
1- data?['isDayTime']
to (data?['isDayTime']?? false)
2- I think the wrong in variable (isDayTime) so you shouldn't use (late) key so if you are sure 100% you will declare this variable so remove late and change it to final if it is't changing again
3- convert this Map? data={}; to Map data={}; because this is already declared and look at this link https://dart.dev/null-safety/understanding-null-safety
CodePudding user response:
Your data is of type Map, and if the data does not have isDayTime field than it is returning null and, ternary operator decision can not be made on null. So, to fix this either you can check whether your data map have the filed using data.containsKey('isDayTime') or You can use ??(null aware operator) to assign a fallback value, so that your ternary operator will always have boolean value.