首页 > 解决方案 > 类型'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));
}

标签: androiddartflutter

解决方案


title: new Text(time ?? "Empty"),

应该

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

或者

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

推荐阅读