gpt4 book ai didi

json - Flutter:显示从服务接收的嵌套数据:已编辑#3

转载 作者:行者123 更新时间:2023-12-03 03:50:14 24 4
gpt4 key购买 nike

我有一个应用程序在页面上从服务器接收嵌套数据,我在打印数据,并成功打印:
上课页面:

  final DateTime mDate;
final List<Games> games;

class DatedMatchs {
DatedMatchs(
this.mDate,
this.games,
);
}
class Games {
Games(
this.id,this.sId,this.wId,this.leagueName,this.homeTeam,this.awayTeam,this.homeGoals,this.awayGoals,this.mHour,this.homeEx,this.awayEx,
);
final String id;
final String sId;
final String wId;
final String leagueName;
final String homeTeam;
final String awayTeam;
final String homeGoals;
final String awayGoals;
final String mHour;
final String homeEx;
final String awayEx;
}
我要显示数据的页面:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
import '../models/dated_matchs.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
List matchs = [];
Future<List> getmatchs() async {
var url =
'xxx/api/controller/matchs/dated_matchs.php?s_id=1';
var response = await http.get(url);
var data = jsonDecode(response.body);
print(data);
}
return FutureBuilder(
future: getmatchs(),
builder: (ctx, snapshot) {
return Container();
});
}
}
现在我不知道如何将接收到的数据添加到列表中,然后在列表 View 中显示它
我在将来的函数中使用了这种方式,但是出了点问题:
Future<List> getmatchs() async {
var url =
'xxx/api/controller/matchs/dated_matchs.php?s_id=1';
var response = await http.get(url);
var data = jsonDecode(response.body);
for (var x in data) {
for (var y in x['games']) {
cont1.add(TextEditingController());
cont2.add(TextEditingController());
Games newmatch = Games(
y['id'],
y['s_id'],
y['w_id'],
y['league_name'],
y['home_team'],
y['away_team'],
y['home_goals'],
y['away_goals'],
y['m_hour'],
y['home_ex'],
y['away_ex']);
matchs.add(newmatch);
}
DatedMatchs newdated = DatedMatchs(x['m_date'], x['matchs']);
datedmatchs.add(newdated);
}
return datedmatchs;
}
没有东西打印

最佳答案

您的某些数据以 map 而不是列表的形式返回。您需要调试并查看哪些数据是Map,然后可以从Map中打印数据。
另外,我不会在您的用户界面中调用api。最好使用状态管理库,例如Bloc,Provider或RxDart。

关于json - Flutter:显示从服务接收的嵌套数据:已编辑#3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63763555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com