【问题标题】:type 'int' is not a subtype of type 'String' error in Dart类型'int'不是Dart中类型'String'错误的子类型
【发布时间】:2018-05-23 09:23:56
【问题描述】:

print 语句和它下面的任何东西都没有运行,错误消息指出问题是上面以var time 开头的最后一行。我还验证了earthquakes 是一个growableList,这意味着earthquakes[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));
}

【问题讨论】:

  • 异常指向哪一行?
  • 我假设earthquakesMap,而不是List
  • 这是我理解的地图列表。行:此文件的第 14 行。 earthquake.usgs.gov/earthquakes/feed/v1.0/summary/…我还添加了指向repo的链接和帖子的错误号
  • 究竟是哪一行导致了错误?我无法复制。
  • 这让我很困惑。这没有意义。它可能会告诉我其他事情并且不够清楚以正确调试。请再次检查帖子以获取错误的 gif。

标签: android dart flutter


【解决方案1】:
title: new Text(time ?? "Empty"),

应该是

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

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

【讨论】:

  • 我不相信这会按预期工作。如果时间为空,则'${time}' 将评估为"null"(一个字符串),并将用于代替"Empty"
  • 就是这样!!!谢谢你。所以颤振错误还不够明确,对吧? @Stephen 你是对的,它确实显示为空,但我可以解决这个问题。
  • 所以由于时间,一个int,不是一个字符串,如果时间不为空,它就不能转换它。明白了。
【解决方案2】:

正如上述答案所指出的,time 变量是 int,而 Text() 需要 String

可能还有另一个问题: 如果timenull,则可识别空值的运算符?? 将无法按预期工作。因为在time *= 1000 表达式中?? 之前使用了time 变量。

因此,time *= 1000应该被删除,Text应该是这样的

Text(time == null ? "Empty" : '${time * 1000}')

注意:time 在这种情况下没有被修改。

【讨论】:

    【解决方案3】:

    您的 sn-p 中以下行中的代码是:

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

    虽然,它实际上应该如下所示:

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

    【讨论】:

      【解决方案4】:

      虽然最初的问题已经得到很好的回答,但我想展示导致问题的类型问题。

      time 已被声明为 int

      Text() 需要一个字符串

      time ?? "Empty" 计算为time 时,类型为int,这将导致Text() 接收到错误的类型。

      type 'String' is not a subtype of type 'int' 消息显示的任何时候,都存在类型不匹配。

      【讨论】:

        【解决方案5】:

        这是因为您在应该使用字符串的地方使用 int 值。使用 int 值或将其转换为字符串 intValueName.toString();

        【讨论】:

          【解决方案6】:

          检查您的数据类型。您的时间可能是一个字符串。在你的情况下,它是一个 int,所以抛出一个错误。

          解决将Int值转换为String的问题。

          将 Int 值转换为字符串。

          static String checkString(dynamic value) {
              if (value is int) {
                return value.toString();
              } else {
                return value;
              }
            }
          }
          

          然后像 as 一样使用它

          title: Text(checkString(time)),
          

          您也可以在 int 类中使用.toString() 函数。

          title: Text(time.toString(),
          

          您还可以将 int 值转换为 String 并使用它

           String time = "$time”;
          

          【讨论】:

            【解决方案7】:

            只检查你是否在ListBuilder的itemBuilder中改变了(上下文,索引)的顺序就行了

            【讨论】:

            • 虽然这可能会回答问题,但最好包含答案的基本部分,因为它会吸引更多的赞成票。尝试正确格式化您的答案并排除诸如bye之类的词。
            猜你喜欢
            • 2020-03-19
            • 2022-09-27
            • 2020-08-07
            • 1970-01-01
            • 2021-05-10
            • 2020-04-23
            • 2020-12-14
            • 1970-01-01
            • 2019-08-14
            相关资源
            最近更新 更多