【发布时间】:2018-05-23 09:23:56
【问题描述】:
print 语句和它下面的任何东西都没有运行,错误消息指出问题是上面以var time 开头的最后一行。我还验证了earthquakes 是一个growableList,这意味着earthquakes[0] 应该可以毫无问题地运行,但它没有......我做错了什么?如果问题需要更多澄清,请告诉我,我会提供。
链接到错误的gif
链接到 GitHub 上的code
我的代码有问题的部分如下。第 43 行报错。
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
class Quake extends StatefulWidget {
var _data;
Quake(this._data);
@override
State<StatefulWidget> createState() => new QuakeState(_data);
}
class QuakeState extends State<Quake> {
// https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson
// "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson";
var _data;
QuakeState(this._data);
@override
Widget build(BuildContext context) {
// debugPrint(_data['features'].runtimeType.toString());
List earthquakes = _data['features'];
return new Scaffold(
appBar: new AppBar(
title: new Text("Quakes - USGS All Earthquakes"),
backgroundColor: Colors.red,
centerTitle: true,
),
body: new ListView.builder(
itemCount: earthquakes.length,
itemBuilder: (BuildContext context, int index) {
print("${earthquakes[index]}");
var earthquake = earthquakes[index];
var time = earthquake['properties']['time'];
time *= 1000;
//var dateTime = new DateTime.fromMillisecondsSinceEpoch(int.parse(time));
//time = new DateFormat.yMMMMd(dateTime).add_jm();
return new ListTile(
title: new Text(time ?? "Empty"),
);
}));
}
}
Future<Map> getJson(String url) async {
return await http.get(url).then((response) => json.decode(response.body));
}
【问题讨论】:
-
异常指向哪一行?
-
我假设
earthquakes是Map,而不是List -
这是我理解的地图列表。行:此文件的第 14 行。 earthquake.usgs.gov/earthquakes/feed/v1.0/summary/…我还添加了指向repo的链接和帖子的错误号
-
究竟是哪一行导致了错误?我无法复制。
-
这让我很困惑。这没有意义。它可能会告诉我其他事情并且不够清楚以正确调试。请再次检查帖子以获取错误的 gif。