【发布时间】:2021-11-09 22:55:19
【问题描述】:
我正在调用 API 数据并希望在不使用 listview.builder 的情况下显示它,但我面临的错误是数据没有被加载并且加载指示器一直处于活动状态。
例如,当我打开我的应用程序时,数据开始自行加载(因为我正在使用未来)。但它总是加载并且数据没有从 API 中获取。
我正在寻找可以帮助我解决此问题的人?
为此,这里是 myClass 代码。
class _TestState extends State<Test> {
Future<List<dynamic>> getLiveMatches() async {
http.Response response = await http.get(
Uri.parse("https://api.cricket.com.au/matches/2780/50837/live")
);
final Map parseData = await json.decode(response.body.toString());
var matches = parseData['liveMatch'];
return matches;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: EdgeInsets.only(left: 10, right: 10, top: 15),
child: RefreshIndicator(
color: Colors.white,
backgroundColor: Colors.purple,
strokeWidth: 5,
onRefresh: ()async{
getLiveMatches();
},
child: Container(
height: double.infinity,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(6),
topRight: Radius.circular(6)
),
color: Colors.white
),
child: FutureBuilder<List<dynamic>>(
future: getLiveMatches(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<dynamic> matches = snapshot.data;
print(matches);
return Column(
children: [
Text(matches.toString())
],
);
}
return Center(
child: CupertinoActivityIndicator(
animating: true, radius: 10));
}
),
),
),
),
);
}
}
【问题讨论】:
标签: flutter http dart flutter-dependencies