代码之家  ›  专栏  ›  技术社区  ›  ThinkDigital

类型“int”不是dart中类型“string”错误的子类型

  •  3
  • ThinkDigital  · 技术社区  · 8 年前

    print语句及其下的任何内容都不会运行,并且错误消息指出问题是从上一行开始的最后一行 var time . 我也证实了 earthquakes 是一个可增长的列表,这意味着 earthquakes[0] 应该毫无问题地运行,但它不…我做错什么了?如果问题需要进一步澄清,请通知我,我会提供。 链接到 gif 错误 链接到 code 在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));
    }
    
    2 回复  |  直到 8 年前
        1
  •  5
  •   Günter Zöchbauer    8 年前
    title: new Text(time ?? "Empty"),
    

    应该是

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

    title: new Text('${time ?? "Empty"}'),
    
        2
  •  0
  •   scottstoll2017    8 年前

    我没有发现任何错误。不知道该告诉你什么…