微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android-类型’int’不是Dart中类型’String’的子类型错误

Print语句或其下面的任何内容均未运行,并且错误消息指出了问题所在,即以var time开头的最后一行.我还验证了地震是一个可增长的列表,这意味着地震[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));
}

解决方法:

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

应该

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

要么

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐