I am getting Null check operator used on a null value error in this code.
I tried almost every way but it didn’t work, I can send you my working call codes if necessary, thank you in advance.
Future callC() async {
try {
final response = await http.get(Uri.parse("https://v3.football.api-sports.io/standings?league=204&season=2023"), headers: headers);
if (response.statusCode == 200) {
final result = modelSnfLig1FromJson(response.body);
if (mounted) {
setState(() {
Deneme = result;
});
}
return result;
} else {
print(response.statusCode);
}
} catch (e) {
print(e.toString());
}
}
Future<void> fetchData() async {
await callC();
//await callB();
//await callA();
url = Uri.parse(widget.value);
}
@override
void initState() {
super.initState();
fetchData();
}
model class
// To parse this JSON data, do
//
// final modelSnfLig1 = modelSnfLig1FromJson(jsonString);
import 'dart:convert';
ModelSnfLig1 modelSnfLig1FromJson(String str) => ModelSnfLig1.fromJson(json.decode(str));
String modelSnfLig1ToJson(ModelSnfLig1 data) => json.encode(data.toJson());
class ModelSnfLig1 {
String modelSnfLig1Get;
Parameters parameters;
List<dynamic> errors;
int results;
Paging paging;
List<Response> response;
ModelSnfLig1({
required this.modelSnfLig1Get,
required this.parameters,
required this.errors,
required this.results,
required this.paging,
required this.response,
});
factory ModelSnfLig1.fromJson(Map<String, dynamic> json) => ModelSnfLig1(
modelSnfLig1Get: json["get"],
parameters: Parameters.fromJson(json["parameters"]),
errors: List<dynamic>.from(json["errors"].map((x) => x)),
results: json["results"],
paging: Paging.fromJson(json["paging"]),
response: List<Response>.from(json["response"].map((x) => Response.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"get": modelSnfLig1Get,
"parameters": parameters.toJson(),
"errors": List<dynamic>.from(errors.map((x) => x)),
"results": results,
"paging": paging.toJson(),
"response": List<dynamic>.from(response.map((x) => x.toJson())),
};
}
class Paging {
int current;
int total;
Paging({
required this.current,
required this.total,
});
factory Paging.fromJson(Map<String, dynamic> json) => Paging(
current: json["current"],
total: json["total"],
);
Map<String, dynamic> toJson() => {
"current": current,
"total": total,
};
}
class Parameters {
String league;
String season;
Parameters({
required this.league,
required this.season,
});
factory Parameters.fromJson(Map<String, dynamic> json) => Parameters(
league: json["league"],
season: json["season"],
);
Map<String, dynamic> toJson() => {
"league": league,
"season": season,
};
}
class Response {
League league;
Response({
required this.league,
});
factory Response.fromJson(Map<String, dynamic> json) => Response(
league: League.fromJson(json["league"]),
);
Map<String, dynamic> toJson() => {
"league": league.toJson(),
};
}
class League {
int id;
String name;
String country;
String logo;
String flag;
int season;
List<List<Standing>> standings;
League({
required this.id,
required this.name,
required this.country,
required this.logo,
required this.flag,
required this.season,
required this.standings,
});
factory League.fromJson(Map<String, dynamic> json) => League(
id: json["id"],
name: json["name"],
country: json["country"],
logo: json["logo"],
flag: json["flag"],
season: json["season"],
standings: List<List<Standing>>.from(json["standings"].map((x) => List<Standing>.from(x.map((x) => Standing.fromJson(x))))),
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"country": country,
"logo": logo,
"flag": flag,
"season": season,
"standings": List<dynamic>.from(standings.map((x) => List<dynamic>.from(x.map((x) => x.toJson())))),
};
}
class Standing {
int rank;
Team team;
int points;
int goalsDiff;
Group group;
String form;
Status status;
Description? description;
All all;
All home;
All away;
DateTime update;
Standing({
required this.rank,
required this.team,
required this.points,
required this.goalsDiff,
required this.group,
required this.form,
required this.status,
required this.description,
required this.all,
required this.home,
required this.away,
required this.update,
});
factory Standing.fromJson(Map<String, dynamic> json) => Standing(
rank: json["rank"],
team: Team.fromJson(json["team"]),
points: json["points"],
goalsDiff: json["goalsDiff"],
group: groupValues.map[json["group"]]!,
form: json["form"],
status: statusValues.map[json["status"]]!,
description: descriptionValues.map[json["description"]]!,
all: All.fromJson(json["all"]),
home: All.fromJson(json["home"]),
away: All.fromJson(json["away"]),
update: DateTime.parse(json["update"]),
);
Map<String, dynamic> toJson() => {
"rank": rank,
"team": team.toJson(),
"points": points,
"goalsDiff": goalsDiff,
"group": groupValues.reverse[group],
"form": form,
"status": statusValues.reverse[status],
"description": descriptionValues.reverse[description],
"all": all.toJson(),
"home": home.toJson(),
"away": away.toJson(),
"update": update.toIso8601String(),
};
}
class All {
int played;
int win;
int draw;
int lose;
Goals goals;
All({
required this.played,
required this.win,
required this.draw,
required this.lose,
required this.goals,
});
factory All.fromJson(Map<String, dynamic> json) => All(
played: json["played"],
win: json["win"],
draw: json["draw"],
lose: json["lose"],
goals: Goals.fromJson(json["goals"]),
);
Map<String, dynamic> toJson() => {
"played": played,
"win": win,
"draw": draw,
"lose": lose,
"goals": goals.toJson(),
};
}
class Goals {
int goalsFor;
int against;
Goals({
required this.goalsFor,
required this.against,
});
factory Goals.fromJson(Map<String, dynamic> json) => Goals(
goalsFor: json["for"],
against: json["against"],
);
Map<String, dynamic> toJson() => {
"for": goalsFor,
"against": against,
};
}
enum Description {
PROMOTION,
PROMOTION_PLAY_OFF,
RELEGATION
}
final descriptionValues = EnumValues({
"Promotion": Description.PROMOTION,
"Promotion Play-off": Description.PROMOTION_PLAY_OFF,
"Relegation": Description.RELEGATION
});
enum Group {
THE_1_LIG_REGULAR_SEASON
}
final groupValues = EnumValues({
"1. Lig: Regular Season": Group.THE_1_LIG_REGULAR_SEASON
});
enum Status {
DOWN,
SAME,
UP
}
final statusValues = EnumValues({
"down": Status.DOWN,
"same": Status.SAME,
"up": Status.UP
});
class Team {
int id;
String name;
String logo;
Team({
required this.id,
required this.name,
required this.logo,
});
factory Team.fromJson(Map<String, dynamic> json) => Team(
id: json["id"],
name: json["name"],
logo: json["logo"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"logo": logo,
};
}
class EnumValues<T> {
Map<String, T> map;
late Map<T, String> reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
reverseMap = map.map((k, v) => MapEntry(v, k));
return reverseMap;
}
}
I checked the incoming json data and it works when I try it in a different call, but it doesn’t work in this code.
2
Answers
Try to use
?? "a default value here"
instead of!
in Standing Model.in this case should be something like this :
I really recommend do that to all the fields so that you avoid the case when a field is null.
Also avoid using
!
as much as you can, I think. Hopefully this help.Make all properties optional like
when you create (by quicktype.io) a model class according to your JSON file try to check null safety and make all properties optional.
I hope this will help you. Thanks