我正在创建一个应用程序来跟踪足球比赛的结果与外部API。我创建的模型提供了实时匹配属性,您可以在下面看到:
import 'package:freezed_annotation/freezed_annotation.dart';
part 'live_match_tile_model.freezed.dart';
part 'live_match_tile_model.g.dart';
@freezed
class LiveMatchTileModel with _$LiveMatchTileModel {
const factory LiveMatchTileModel({
Fixture? fixture,
League? league,
Goals? teams,
Goals? goals,
Score? score,
List<Event>? events,
}) = _LiveMatchTileModel;
factory LiveMatchTileModel.fromJson(Map<String, dynamic> json) => _$LiveMatchTileModelFromJson(json);
}
@freezed
class Event with _$Event {
const factory Event({
Time? time,
Team? team,
Assist? player,
Assist? assist,
String? type,
String? detail,
dynamic comments,
}) = _Event;
factory Event.fromJson(Map<String, dynamic> json) => _$EventFromJson(json);
}
@freezed
class Assist with _$Assist {
const factory Assist({
dynamic id,
dynamic name,
}) = _Assist;
factory Assist.fromJson(Map<String, dynamic> json) => _$AssistFromJson(json);
}
@freezed
class Team with _$Team {
const factory Team({
int? id,
String? name,
String? logo,
dynamic winner,
}) = _Team;
factory Team.fromJson(Map<String, dynamic> json) => _$TeamFromJson(json);
}
@freezed
class Time with _$Time {
const factory Time({
int? elapsed,
dynamic extra,
}) = _Time;
factory Time.fromJson(Map<String, dynamic> json) => _$TimeFromJson(json);
}
@freezed
class Fixture with _$Fixture {
const factory Fixture({
int? id,
dynamic referee,
String? timezone,
DateTime? date,
int? timestamp,
Periods? periods,
Venue? venue,
Status? status,
}) = _Fixture;
factory Fixture.fromJson(Map<String, dynamic> json) => _$FixtureFromJson(json);
}
@freezed
class Periods with _$Periods {
const factory Periods({
int? first,
dynamic second,
}) = _Periods;
factory Periods.fromJson(Map<String, dynamic> json) => _$PeriodsFromJson(json);
}
@freezed
class Status with _$Status {
const factory Status({
String? long,
String? short,
int? elapsed,
}) = _Status;
factory Status.fromJson(Map<String, dynamic> json) => _$StatusFromJson(json);
}
@freezed
class Venue with _$Venue {
const factory Venue({
int? id,
String? name,
String? city,
}) = _Venue;
factory Venue.fromJson(Map<String, dynamic> json) => _$VenueFromJson(json);
}
@freezed
class Goals with _$Goals {
const factory Goals({
dynamic home,
dynamic away,
}) = _Goals;
factory Goals.fromJson(Map<String, dynamic> json) => _$GoalsFromJson(json);
}
@freezed
class League with _$League {
const factory League({
int? id,
String? name,
String? country,
String? logo,
String? flag,
int? season,
String? round,
}) = _League;
factory League.fromJson(Map<String, dynamic> json) => _$LeagueFromJson(json);
}
@freezed
class Score with _$Score {
const factory Score({
Goals? halftime,
Goals? fulltime,
Goals? extratime,
Goals? penalty,
}) = _Score;
factory Score.fromJson(Map<String, dynamic> json) => _$ScoreFromJson(json);
}
我使用PrettyDioLogger
来检查响应,一切似乎都在工作,但提供的数据没有显示的UI。当我试图检查出了什么问题时,我注意到我的cubit函数得到的结果告诉我:
“类型'Null'不是类型强制转换中类型Map<String,dynamic>的子类型”。
下面是Cubit结构:
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:score_live/repositories/home_screen_repository.dart';
import 'package:score_live/core/enums.dart';
import 'package:score_live/models/live_match_tile_model.dart';
part 'home_state.dart';
part 'home_cubit.freezed.dart';
class HomeCubit extends Cubit<HomeState> {
HomeCubit(this.homeScreenRepository) : super(const HomeState());
final HomeScreenRepository homeScreenRepository;
Future<void> fetchLiveMatches() async {
try {
final liveMatches = await homeScreenRepository.fetchLiveMatches();
emit(state.copyWith(liveMatchTileModel: liveMatches));
} catch (error) {
emit(state.copyWith(errorMessage: error.toString()));
print(error); // Here app prints the previously mentioned error in debug console
}
}
}
我试图为生成的模型的动态属性给予默认值,或者将它们的类型更改为动态之外的类型,添加“!“和零感知““在使用API数据的地方进行检查,JSON键也没有帮助。以下是API响应的示例:
{
"get": "fixtures",
"parameters": {
"live": "all"
},
"errors": [],
"results": 35,
"paging": {
"current": 1,
"total": 1
},
"response": [
{
"fixture": {
"id": 999014,
"referee": null,
"timezone": "UTC",
"date": "2023-06-28T15:30:00+00:00",
"timestamp": 1687966200,
"periods": {
"first": 1687966200,
"second": null
},
"venue": {
"id": 6351,
"name": "Järvenpään keskuskenttä",
"city": "Järvenpää"
},
"status": {
"long": "First Half",
"short": "1H",
"elapsed": 12
}
},
"league": {
"id": 245,
"name": "Ykkönen",
"country": "Finland",
"logo": "https://media-2.api-sports.io/football/leagues/245.png",
"flag": "https://media-1.api-sports.io/flags/fi.svg",
"season": 2023,
"round": "Regular Season - 12"
},
"teams": {
"home": {
"id": 9173,
"name": "JäPS",
"logo": "https://media-2.api-sports.io/football/teams/9173.png",
"winner": null
},
"away": {
"id": 9189,
"name": "SalPa",
"logo": "https://media-3.api-sports.io/football/teams/9189.png",
"winner": null
}
},
"goals": {
"home": 0,
"away": 0
},
"score": {
"halftime": {
"home": 0,
"away": 0
},
"fulltime": {
"home": null,
"away": null
},
"extratime": {
"home": null,
"away": null
},
"penalty": {
"home": null,
"away": null
}
},
"events": [
{
"time": {
"elapsed": 7,
"extra": null
},
"team": {
"id": 9189,
"name": "SalPa",
"logo": "https://media-3.api-sports.io/football/teams/9189.png"
},
"player": {
"id": null,
"name": null
},
"assist": {
"id": null,
"name": null
},
"type": "Card",
"detail": "Yellow Card",
"comments": null
}
]}]
}
1条答案
按热度按时间nfs0ujit1#
您的方法
fromJson
,如:和其他人,不要接受null值,你需要这样做:
在参数中加上“?”,所以,即使你的变量接受为null,你的方法也不接受。