类型'int'不是Dart中类型'String'错误的子类型

新手上路,请多包涵

打印语句及其下方的任何内容均未运行,并且错误消息指出问题是上面以 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));
}

原文由 ThinkDigital 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 338
2 个回答
title: new Text(time ?? "Empty"),

应该

title: new Text(time != null ? '$time' : "Empty"),

或者

title: new Text('${time ?? "Empty"}'),

原文由 Günter Zöchbauer 发布,翻译遵循 CC BY-SA 4.0 许可协议

这是因为您正在使用应该使用字符串的 int 值。使用 int 值或将其转换为字符串 intValueName.toString();

原文由 Muhammad Umair Saqib 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题