android - 类型'int'不是Dart中类型'String'错误的子类型
问题描述
print 语句和它下面的任何东西都没有运行,并且错误消息指出问题是上面以 . 开头的最后一行var time
。我还验证了它earthquakes
是一个增长列表,这意味着它earthquakes[0]
应该可以毫无问题地运行,但它没有......我做错了什么?如果问题需要更多澄清,请告诉我,我会提供。链接到错误的gif链接到GitHub 上的代码
我的代码有问题的部分如下。第 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));
}
解决方案
title: new Text(time ?? "Empty"),
应该
title: new Text(time != null ? '$time' : "Empty"),
或者
title: new Text('${time ?? "Empty"}'),
推荐阅读
- python - 无论python版本如何,conda都拒绝安装
- c# - 为什么选择这种通用方法而不是更好的匹配?
- c# - c#中如何比较一维数组和二维数组
- kotlin - Mockito 如何验证分配?
- python - 试图创建一个类来减少重复代码,但由于缺乏更好的术语而陷入枚举问题
- mysql - 每次我选择它时,MySQL 函数的执行时间都会增加
- excel - 如何在 MS-Excel 中连续复制多个单元格?
- python-3.x - Google translate API Text is too long 错误
- excel - 从单元格中找到一个字符串,并删除附加到它的 4-5 行,然后移动到下一个单元格
- html - 来自字符串的 Dart 服务器响应 HTML