Reputation: 3539
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));
}
Upvotes: 23
Views: 162826
Reputation: 1865
This is because you are using the int value where the string should be used. Use int value or cast it to string as intValueName.toString();
Upvotes: 2
Reputation: 9
just check if you change the order of (context, index) at the itemBuilder of ListBuilder bye
Upvotes: -1
Reputation: 413
As the above answers pointed, the time
variable was int
while the Text()
required an String
.
There might be another issue:
If the time
was null
, the null-aware operator ??
will not work as expected. Because the time
variable was used before the ??
in time *= 1000
expression.
Therefore, the time *= 1000
should be deleted, and the Text
should be like
Text(time == null ? "Empty" : '${time * 1000}')
Note: The time
was not modified in this case.
Upvotes: 3
Reputation: 4735
Although the original question has been well answered, I want to show the type issue that caused the problem.
time
has been declared as an int
Text()
requires a String
When the time ?? "Empty"
evaluates to time
the type is int, which will cause the Text()
to receive the wrong type.
Anytime the type 'String' is not a subtype of type 'int'
message shows, there has been a type mismatch.
Upvotes: 2
Reputation: 5005
The code in following line from your snippet is:
title: new Text(time ?? "Empty"),
While, it should actually look like following:
title: new Text(time?.toString() ?? "Empty"),
Upvotes: 2
Reputation: 658205
title: new Text(time ?? "Empty"),
should be
title: new Text(time != null ? '$time' : "Empty"),
or
title: new Text('${time ?? "Empty"}'),
Upvotes: 22