Error: Exception caught by widgets library, Null check operator used on a null value The relevant error-causing widget was FutureBuilder<WorldStatsModel
This is the json code:
class WorldStatsModel
{
int? updated;
int? cases;
int? todayCases;
int? deaths;
int? todayDeaths;
int? recovered;
int? todayRecovered;
int? active;
int? critical;
int? casesPerOneMillion;
double? deathsPerOneMillion;
int? tests;
double? testsPerOneMillion;
int? population;
int? oneCasePerPeople;
int? oneDeathPerPeople;
int? oneTestPerPeople;
double? activePerOneMillion;
double? recoveredPerOneMillion;
double? criticalPerOneMillion;
int? affectedCountries;
WorldStatsModel(
{this.updated,
this.cases,
this.todayCases,
this.deaths,
this.todayDeaths,
this.recovered,
this.todayRecovered,
this.active,
this.critical,
this.casesPerOneMillion,
this.deathsPerOneMillion,
this.tests,
this.testsPerOneMillion,
this.population,
this.oneCasePerPeople,
this.oneDeathPerPeople,
this.oneTestPerPeople,
this.activePerOneMillion,
this.recoveredPerOneMillion,
this.criticalPerOneMillion,
this.affectedCountries});
WorldStatsModel.fromJson(Map<String, dynamic> json) {
updated = json['updated'];
cases = json['cases'];
todayCases = json['todayCases'];
deaths = json['deaths'];
todayDeaths = json['todayDeaths'];
recovered = json['recovered'];
todayRecovered = json['todayRecovered'];
active = json['active'];
critical = json['critical'];
casesPerOneMillion = json['casesPerOneMillion'];
deathsPerOneMillion = json['deathsPerOneMillion'];
tests = json['tests'];
testsPerOneMillion = json['testsPerOneMillion'];
population = json['population'];
oneCasePerPeople = json['oneCasePerPeople'];
oneDeathPerPeople = json['oneDeathPerPeople'];
oneTestPerPeople = json['oneTestPerPeople'];
activePerOneMillion = json['activePerOneMillion'];
recoveredPerOneMillion = json['recoveredPerOneMillion'];
criticalPerOneMillion = json['criticalPerOneMillion'];
affectedCountries = json['affectedCountries'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['updated'] = this.updated;
data['cases'] = this.cases;
data['todayCases'] = this.todayCases;
data['deaths'] = this.deaths;
data['todayDeaths'] = this.todayDeaths;
data['recovered'] = this.recovered;
data['todayRecovered'] = this.todayRecovered;
data['active'] = this.active;
data['critical'] = this.critical;
data['casesPerOneMillion'] = this.casesPerOneMillion;
data['deathsPerOneMillion'] = this.deathsPerOneMillion;
data['tests'] = this.tests;
data['testsPerOneMillion'] = this.testsPerOneMillion;
data['population'] = this.population;
data['oneCasePerPeople'] = this.oneCasePerPeople;
data['oneDeathPerPeople'] = this.oneDeathPerPeople;
data['oneTestPerPeople'] = this.oneTestPerPeople;
data['activePerOneMillion'] = this.activePerOneMillion;
data['recoveredPerOneMillion'] = this.recoveredPerOneMillion;
data['criticalPerOneMillion'] = this.criticalPerOneMillion;
data['affectedCountries'] = this.affectedCountries;
return data;
}
}
This is the future builder function where it is showing the error:
FutureBuilder(
future: statsServices.fetchWorldStatesRecords(),
builder: (context, AsyncSnapshot<WorldStatsModel> snapshot) {
if (!snapshot.hasData) {
return Column(
children: [
PieChart(
dataMap: {
'cases':
double.parse(snapshot.data!.cases.toString()),
'recovered': double.parse(
snapshot.data!.recovered.toString()),
'deaths': double.parse(
snapshot.data!.deaths.toString()),
},
chartValuesOptions: const ChartValuesOptions(
showChartValuesInPercentage: true),
chartRadius:
MediaQuery.of(context).size.width / 3.2,
legendOptions: const LegendOptions(
legendPosition: LegendPosition.left),
animationDuration: const Duration(seconds: 3),
chartType: ChartType.ring,
colorList: colorlist,
),
const SizedBox(
height: 15,
),
Card(
child: Column(children: [
ReusableRow(
title: 'updated',
value: snapshot.data!.updated.toString()),
ReusableRow(
title: 'cases',
value: snapshot.data!.cases.toString()),
ReusableRow(
title: 'todayCases',
value: snapshot.data!.todayCases.toString()),
ReusableRow(
title: 'deaths',
value: snapshot.data!.deaths.toString()),
ReusableRow(
title: 'todayDeaths',
value: snapshot.data!.todayDeaths.toString()),
ReusableRow(
title: 'recovered',
value: snapshot.data!.recovered.toString()),
ReusableRow(
title: 'todayRecovered',
value:
snapshot.data!.todayRecovered.toString()),
ReusableRow(
title: 'active',
value: snapshot.data!.active.toString()),
ReusableRow(
title: 'critical',
value: snapshot.data!.critical.toString()),
ReusableRow(
title: 'casesPerOneMillion',
value: snapshot.data!.casesPerOneMillion
.toString()),
ReusableRow(
title: 'deathsPerOneMillion',
value: snapshot.data!.deathsPerOneMillion
.toString()),
ReusableRow(
title: 'tests',
value: snapshot.data!.tests.toString()),
ReusableRow(
title: 'testsPerOneMillion',
value: snapshot.data!.testsPerOneMillion
.toString()),
ReusableRow(
title: 'population',
value: snapshot.data!.population.toString()),
ReusableRow(
title: 'affectedCountries',
value: snapshot.data!.affectedCountries
.toString()),
ReusableRow(
title: 'recoveredPerOneMillion',
value: snapshot.data!.recoveredPerOneMillion
.toString()),
]),
),
const SizedBox(
height: 20,
),
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const CountryList(),
));
},
child: Container(
height: 50,
width: 300,
decoration: BoxDecoration(
color: const Color(0xff1aa260),
borderRadius: BorderRadius.circular(10),
boxShadow: const [
BoxShadow(
color: Colors.black,
blurRadius: 2,
)
]),
child: const Center(
child: Text(
'Track Country',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.black),
),
),
),
),
const SizedBox(
height: 10,
),
],
);
} else {
return Center(
child: SpinKitFadingCircle(
color: Colors.black,
size: 50,
controller: controller,
),
);
}
}),
This is the class in which I have fetched the Api:
class StatsServices {
Future<WorldStatsModel> fetchWorldStatesRecords() async {
final response = await http.get(Uri.parse(AppUrl.worldstatsApi));
if (response.statusCode == 200) {
var data = jsonDecode(response.body.toString());
return WorldStatsModel.fromJson(data);
} else {
throw Exception('Error');
}
}
This is the separate class for the URL:
class AppUrl {
static const String baseurl = 'https://disease.sh/v3/covid-19/';
static const String worldstatsApi = baseurl 'all';
static const String countrylist = baseurl 'countries';
}
CodePudding user response:
here your error
if (!snapshot.hasData) {
you put !
before snapshot.hasData ,that make the wrong logic. remove it.
like this
if (snapshot.hasData) {
CodePudding user response:
Error Explanation: Bang operator(!) AKA Null check operator or Null aware operator, means in flutter, when you use this operator, you are completely sure that variable is not going to be null in any case.
Now whenever we encounter Null check/aware operator used on a null value There are two ways to resolve it peacefully -
- Use if conditional to confirm that variable is not null
- Use null-aware or if-null operator ?? like
//1.
if (foo != null) {
String otherStr = foo;
print(otherStr);
}
//2.
String? foo;
String str = foo ?? "dummy";
print(str);
Since you provided partial error; based on provided base url when you hit https://disease.sh/v3/covid-19/updated
, we do not receive expected response hence your error is at snapshot.data!
. So before using !
make sure your variable is not null
.