【发布时间】:2018-11-12 09:15:49
【问题描述】:
我一直在关注this youtube video,以帮助我将在线 JSON 文件中的数据返回到列表视图中。我稍微修改了代码,包括更改 JSON 文件的 URL,因此,代码现在请求不同的数据。
某事告诉我这是因为我想使用的 JSON 类型与我使用的代码不兼容,但我不知道为什么并且可能是错误的。我使用所提供视频的作者使用的原始 'StarWarsData'、'StarWarsState' 只是为了尽量减少我的代码中的差异。
谢谢,杰克
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() {
runApp(MaterialApp(
home: StarWarsData(),
));
}
class StarWarsData extends StatefulWidget {
@override
StarWarsState createState() => StarWarsState();
}
class StarWarsState extends State<StarWarsData> {
final String url = "https://api.coinmarketcap.com/v2/listings/";
List data;
Future<String> getSWData() async {
var res = await http
.get(Uri.encodeFull(url), headers: {"Accept": "application/json"});
setState(() {
var resBody = json.decode(res.body);
data = resBody["data"];
});
return "Success!";
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Star Wars Starships"),
backgroundColor: Colors.deepPurpleAccent,
),
body: ListView.builder(
itemCount: data == null ? 0 : data.length,
itemBuilder: (BuildContext context, int index) {
return new Container(
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Card(
child: Container(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Text("Id: "),
Text(data[index]["id"],
style: TextStyle(
fontSize: 18.0, color: Colors.black87)),
],
)),
),
Card(
child: Container(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Text("Name: "),
Text(data[index]["name"],
style: TextStyle(
fontSize: 18.0, color: Colors.red)),
],
)),
),
Card(
child: Container(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Text("Symbol: "),
Text(data[index]["symbol"],
style: TextStyle(
fontSize: 18.0, color: Colors.black87)),
],
)),
),
],
),
),
);
},
),
);
}
@override
void initState() {
super.initState();
this.getSWData();
}
}
编辑
这个问题现在已经解决了,但是如果有人感兴趣,这里是我之前遇到的错误:
I/flutter (31850): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════I/flutter
(31850): type 'int' is not a subtype of type 'String' where I/flutter
(31850): int is from dart:core I/flutter
(31850): String is from dart:core
【问题讨论】:
-
请添加您收到的错误信息
-
问题已经得到解答,但以防万一您仍然感兴趣,这里是错误。 I/flutter (31850): ══╡ 小部件库发现异常╞════════════════════════════════════ ════════════════════════════I/flutter(31850):类型'int'不是类型'String'的子类型,其中I /flutter (31850): int 来自 dart:core I/flutter (31850): String 来自 dart:core
-
如果这还不够,请关注我,我的回复空间有限
-
您可以编辑问题并添加其他信息。这样你也可以正确格式化它。
-
那么你想让我重新格式化问题还是给你更多我的错误?我想我应该编辑我的问题并添加完整的日志
标签: android ios json dart flutter