Api 颤振:未来构建器从Calss接收空值
我正在尝试制作live score应用程序,我有一个由quiqtype.io从json创建的模型:Api 颤振:未来构建器从Calss接收空值,api,flutter,dart,Api,Flutter,Dart,我正在尝试制作live score应用程序,我有一个由quiqtype.io从json创建的模型: import 'dart:convert'; Live liveFromJson(String str) => Live.fromJson(json.decode(str)); String liveToJson(Live data) => json.encode(data.toJson()); class Live { Live({ this.success,
import 'dart:convert';
Live liveFromJson(String str) => Live.fromJson(json.decode(str));
String liveToJson(Live data) => json.encode(data.toJson());
class Live {
Live({
this.success,
this.data,
});
bool success;
Data data;
factory Live.fromJson(Map<String, dynamic> json) => Live(
success: json["success"],
data: Data.fromJson(json["data"]),
);
Map<String, dynamic> toJson() => {
"success": success,
"data": data.toJson(),
};
}
class Data {
Data({
this.fixtures,
this.nextPage,
this.prevPage,
});
List<Fixture> fixtures;
String nextPage;
bool prevPage;
factory Data.fromJson(Map<String, dynamic> json) => Data(
fixtures: List<Fixture>.from(
json["fixtures"].map((x) => Fixture.fromJson(x))),
nextPage: json["next_page"],
prevPage: json["prev_page"],
);
Map<String, dynamic> toJson() => {
"fixtures": List<dynamic>.from(fixtures.map((x) => x.toJson())),
"next_page": nextPage,
"prev_page": prevPage,
};
}
class Fixture {
Fixture({
this.id,
this.date,
this.time,
this.round,
this.homeName,
this.awayName,
this.location,
this.leagueId,
this.competitionId,
this.homeId,
this.awayId,
this.competition,
this.league,
});
String id;
DateTime date;
String time;
String round;
String homeName;
String awayName;
String location;
String leagueId;
String competitionId;
String homeId;
String awayId;
Competition competition;
League league;
factory Fixture.fromJson(Map<String, dynamic> json) => Fixture(
id: json["id"],
date: DateTime.parse(json["date"]),
time: json["time"],
round: json["round"],
homeName: json["home_name"],
awayName: json["away_name"],
location: json["location"],
leagueId: json["league_id"],
competitionId: json["competition_id"],
homeId: json["home_id"],
awayId: json["away_id"],
competition: Competition.fromJson(json["competition"]),
league: json["league"] == null ? null : League.fromJson(json["league"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"date":
"${date.year.toString().padLeft(4, '0')}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}",
"time": time,
"round": round,
"home_name": homeName,
"away_name": awayName,
"location": location,
"league_id": leagueId,
"competition_id": competitionId,
"home_id": homeId,
"away_id": awayId,
"competition": competition.toJson(),
"league": league == null ? null : league.toJson(),
};
}
class Competition {
Competition({
this.id,
this.name,
});
String id;
String name;
factory Competition.fromJson(Map<String, dynamic> json) => Competition(
id: json["id"],
name: json["name"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
};
}
class League {
League({
this.id,
this.name,
this.countryId,
});
String id;
String name;
String countryId;
factory League.fromJson(Map<String, dynamic> json) => League(
id: json["id"],
name: json["name"],
countryId: json["country_id"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"country_id": countryId,
};
}
导入'dart:convert';
Live-liveFromJson(String-str)=>Live.fromJson(json.decode(str));
字符串liveToJson(livedata)=>json.encode(data.toJson());
课堂直播{
生活({
这就是成功,
这个数据,,
});
成功;
数据;
factory Live.fromJson(映射json)=>Live(
success:json[“success”],
data:data.fromJson(json[“data”]),
);
映射到JSON()=>{
“成功”:成功,
“data”:data.toJson(),
};
}
类数据{
资料({
这是固定装置,
这是下一页,
本页,
});
列出固定装置清单;
字符串下一页;
布尔普雷瓦奇;
工厂数据.fromJson(映射json)=>数据(
fixtures:List.from(
json[“fixtures”].map((x)=>Fixture.fromJson(x)),
下一页:json[“下一页”],
prevPage:json[“prev_page”],
);
映射到JSON()=>{
“fixtures”:List.from(fixtures.map((x)=>x.toJson()),
“下一页”:下一页,
“上一页”:上一页,
};
}
班级固定装置{
固定装置({
这个身份证,
这个日期,
这次,
这一轮,
这是我的名字,
这是awayName,
这个位置,
这个,联盟,
这是一场竞争,
这是霍梅德,
这个,阿瓦伊德,
这个比赛,,
这个联盟,
});
字符串id;
日期时间日期;
串时间;
串圆;
字符串homeName;
字符串awayName;
字符串位置;
弦乐联盟;
弦乐比赛;
字符串homeId;
串起;
竞争;
团;
factory Fixture.fromJson(映射json)=>Fixture(
id:json[“id”],
date:DateTime.parse(json[“date”]),
时间:json[“时间”],
round:json[“round”],
homeName:json[“home_name”],
awayName:json[“away_name”],
位置:json[“位置”],
leagueId:json[“league_id”],
competitionId:json[“competition_id”],
homeId:json[“home\u id”],
awayId:json[“away\u id”],
competition:competition.fromJson(json[“competition”]),
league:json[“league”]==null?null:league.fromJson(json[“league”]),
);
映射到JSON()=>{
“id”:id,
“日期”:
“${date.year.toString().padLeft(4,'0')}-${date.month.toString().padLeft(2,'0')}-${date.day.toString().padLeft(2,'0')}”,
“时间”:时间,
“圆”:圆,
“home_name”:home name,
“away_name”:awayName,
“位置”:位置,
“联盟id”:联盟id,
“竞争id”:竞争id,
“家庭id”:家庭id,
“awayId”:awayId,
“competition”:competition.toJson(),
“league”:league==null?null:league.toJson(),
};
}
班级竞赛{
竞争({
这个身份证,
这个名字,
});
字符串id;
字符串名;
工厂竞争。fromJson(Map json)=>竞争(
id:json[“id”],
名称:json[“名称”],
);
映射到JSON()=>{
“id”:id,
“姓名”:姓名,
};
}
阶级联盟{
联盟({
这个身份证,
这个名字,
这是countryId,
});
字符串id;
字符串名;
字符串countryId;
工厂联盟.fromJson(映射json)=>联盟(
id:json[“id”],
名称:json[“名称”],
countryId:json[“国家/地区id”],
);
映射到JSON()=>{
“id”:id,
“姓名”:姓名,
“国家id”:国家id,
};
}
i创建API服务类:
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:myexpect/models/match_fixture.dart';
class Api {
Future<Live> get_fixture() async {
var fixture_model = null;
var client = http.Client();
try {
var url =
'https://livescore-api.com/api-client/fixtures/matches.json?key=xxxxxxC&secret=yyyyy&page=1';
var response = await client.get(url);
if (response.statusCode == 200) {
var jsonString = response.body;
var jsonMap = json.decode(jsonString);
fixture_model = Live.fromJson(jsonMap);
print(jsonMap ): //--- printed the list in console successfully
}
} catch (Exception) {
return fixture_model;
}
}
}
导入'dart:convert';
将“package:http/http.dart”导入为http;
导入“package:myexpect/models/match_fixture.dart”;
类Api{
Future get_fixture()异步{
var\u model=null;
var client=http.client();
试一试{
变量url=
'https://livescore-api.com/api-client/fixtures/matches.json?key=xxxxxxC&secret=yyyyy&page=1';
var response=wait client.get(url);
如果(response.statusCode==200){
var jsonString=response.body;
var jsonMap=json.decode(jsonString);
fixture_model=Live.fromJson(jsonMap);
打印(jsonMap)://---在控制台中成功打印列表
}
}捕获(例外){
回归模型;
}
}
}
我现在尝试在此页面中查看未来建筑中的此数据:
import 'package:flutter/material.dart';
import '../services/live_score_api.dart';
import '../models/match_fixture.dart';
class LiveScore extends StatefulWidget {
@override
_LiveScoreState createState() => _LiveScoreState();
}
class _LiveScoreState extends State<LiveScore> {
Future<Live> _fixtures;
@override
void initState() {
_fixtures = Api().get_fixture();
super.initState();
}
@override
Widget build(BuildContext context) {
return FutureBuilder<Live>(
future: _fixtures,
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.none ||
snapshot.connectionState == ConnectionState.waiting ||
snapshot.connectionState == ConnectionState.active ||
snapshot.data == null) {
return Container(
child: Text('Loading.......'),
);
} else {
return ListView.builder(
itemCount: snapshot.data.data.fixtures.length,
itemBuilder: (ctx, index) {
var data = snapshot.data.data.fixtures[index];
return Text(data.time);
});
}
},
);
}
}
导入“包装:颤振/材料.省道”;
导入“../services/live_score_api.dart”;
导入“../models/match_fixture.dart”;
类LiveScore扩展了StatefulWidget{
@凌驾
_LiveScoreState createState()=>\u LiveScoreState();
}
类_LiveScoreState扩展状态{
未来固定装置;
@凌驾
void initState(){
_fixtures=Api()。获取fixture();
super.initState();
}
@凌驾
小部件构建(构建上下文){
回归未来建设者(
未来:_装置,
生成器:(ctx,快照){
如果(snapshot.connectionState==connectionState.none||
snapshot.connectionState==connectionState.waiting||
snapshot.connectionState==connectionState.active||
snapshot.data==null){
返回容器(
子项:文本('Loading…'),
);
}否则{
返回ListView.builder(
itemCount:snapshot.data.data.fixtures.length,
itemBuilder:(ctx,索引){
var data=snapshot.data.data.fixtures[index];
返回文本(data.time);
});
}
},
);
}
}
当我加载此页面时,控制台上成功打印的数据列表,但future builder接收到空值,因此只查看文本“Loading…”,未发现任何错误和异常。是否要实现future builder,如下所示:
返回FutureBuilder(
未来:_装置,
生成器:(上下文,快照){
交换机(快照.连接状态){
案例连接状态。无:
案例连接状态。正在等待:
case ConnectionState.activ
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:myexpect/models/match_fixture.dart';
class Api {
Future<Live> get_fixture() async {
var fixture_model = null;
var client = http.Client();
try {
var url =
'https://livescore-api.com/api-client/fixtures/matches.json?key=xxxxxxC&secret=yyyyy&page=1';
var response = await client.get(url);
if (response.statusCode == 200) {
var jsonString = response.body;
var jsonMap = json.decode(jsonString);
fixture_model = Live.fromJson(jsonMap);
print(jsonMap ): //--- printed the list in console successfully
}
} catch (Exception) {
return fixture_model;
}
}
}
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:myexpect/models/match_fixture.dart';
class Api {
Future<Live> get_fixture() async {
var fixture_model = null;
var client = http.Client();
try {
var url =
'https://livescore-api.com/api-client/fixtures/matches.json?key=xxxxxxC&secret=yyyyy&page=1';
var response = await client.get(url);
if (response.statusCode == 200) {
var jsonString = response.body;
var jsonMap = json.decode(jsonString);
fixture_model = Live.fromJson(jsonMap);
print(jsonMap ): //--- printed the list in console successfully
}
} catch (Exception) {
return fixture_model;
}
return fixture_model; //------------I added this line that i forgot :)
}
}