Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type 'int' is not a subtype of type 'String' error in Dart

Neither The print statement nor anything below it run, and the error message points to the issue being the last line above starting with var time. I also verified that earthquakes is a growableList, which means that earthquakes[0] should run without issue, but it doesn't... What am I doing wrong? Let me know if the question needs more clarification and I'll provide it. Link to gif of error Link to code on GitHub

The problematic part of my code is as follows. Error reported on line 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));
}
like image 285
ThinkDigital Avatar asked May 23 '18 09:05

ThinkDigital


1 Answers

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

should be

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

or

title: new Text('${time ?? "Empty"}'),
like image 144
Günter Zöchbauer Avatar answered Sep 19 '22 12:09

Günter Zöchbauer