Home » Android » android – type 'int' is not a subtype of type 'String' error in Dart

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

Posted by: admin June 15, 2020 Leave a comment

Questions:

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));
}
How to&Answers:
title: new Text(time ?? "Empty"),

should be

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

or

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

Answer:

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"),

Answer:

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.

Answer:

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.

Answer:

I’m not getting any error. Not sure what to tell you…