【发布时间】:2021-02-17 21:38:48
【问题描述】:
编辑-“当我尝试运行 print(snapshot.error) 时,它给出了“int 类型不是字符串类型的子类型””
我正在尝试从https://raw.githubusercontent.com/RahulBagdiOfficial/rto_app_flutter/master/assets/json/applyonline.json获取json数据
使用https请求包解析成json数据,
我正在使用它来使用 ListView.builder 构建一个列表 如果数据为空,则返回 CircularProgressIndicator 如果它包含数据返回列表
问题是这样的
加载时卡住了
这是我的代码
class ApplyOnline extends StatefulWidget {
@override
_ApplyOnlineState createState() => _ApplyOnlineState();
}
class _ApplyOnlineState extends State<ApplyOnline> {
@override
Future<List<ApplyOnlineList>> _getapplyonlinelist() async {
var data = await http.get(
"https://raw.githubusercontent.com/RahulBagdiOfficial/rto_app_flutter/master/assets/json/applyonline.json");
var jsonData = json.decode(data.body);
List<ApplyOnlineList> applyonlinelist = [];
for (var i in jsonData) {
ApplyOnlineList applyonlineobject =
ApplyOnlineList(i['index'], i['string'], i['url']);
applyonlinelist.add(applyonlineobject);
}
print(applyonlinelist.length);
return applyonlinelist;
}
Widget customURLButton(String text, String URL, Icon icon) {
;
}
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xff655ee6),
appBar: AppBar(
backgroundColor: Color(0xff655ee6),
title: Text("Apply Online"),
),
body: SingleChildScrollView(
child: SizedBox(
height: MediaQuery.of(context).size.height,
child: FutureBuilder(
future: _getapplyonlinelist(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return Container(
child: Center(
child: CircularProgressIndicator(),
),
);
} if(snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(snapshot.data[index].string),
);
},
);
}
},
),
),
),
);
}
}
class ApplyOnlineList {
final int index;
final String url;
final String string;
ApplyOnlineList(this.url, this.index, this.string);
}
【问题讨论】:
-
随问题附上日志会很有帮助
-
您好,您确定调用了 _getapplyonlinelist() 的这个函数吗?从我看到它没有被调用,这就是为什么你只有空,尝试将它放在
return Scaffold()之前并尝试打印出来
标签: json database flutter null