【问题标题】:How can I deal with dart casting problem?我该如何处理飞镖投掷问题?
【发布时间】:2019-02-02 06:49:17
【问题描述】:

我必须为我的自定义对象制作 json 数据,下面的代码显示了我的模型。

import 'package:meta/meta.dart';

class KakaoMLModel {
  final List<List<double>> jaw;
  final List<List<double>> rightEyebrow;
  final List<List<double>> leftEyebrow;
  final List<List<double>> nose;
  final List<List<double>> rightEye;
  final List<List<double>> leftEye;
  final List<List<double>> lip;

  KakaoMLModel({
    @required this.jaw,
    @required this.rightEyebrow,
    @required this.leftEyebrow,
    @required this.nose,
    @required this.rightEye,
    @required this.leftEye,
    @required this.lip
  });

  factory KakaoMLModel.fromJson(Map<String,dynamic> json) {
    print(json['jaw']);
    return KakaoMLModel(
      jaw: json['jaw'],
      rightEyebrow: json['rightEyebrow'],
      leftEyebrow: json['leftEyebrow'],
      nose: json['nose'],
      rightEye: json['rightEye'],
      leftEye: json['leftEye'],
      lip: json['lip']
    );
  }
}

但是,当我通过参数传递json数据时,会出现这样的错误消息。

_TypeError (type 'List<dynamic>' is not a subtype of type 'List<List<double>>'

json['jaw'] 格式如下

[[0.04616761798553884, 0.5475042080412023], [0.026029605771987013, 0.6203330820534684], [0.014870009633410218, 0.6947797773981148], [0.01478418759402894, 0.7684331582472369], [0.03166518933198375, 0.8392744747251855], [0.06115956082472804, 0.9024180638379186], [0.10860571254829726, 0.9601635183836916], [0.17011504626717094, 1.0057204991427136], [0.25121165844788507, 1.0340586363828044], [0.3433729801681099, 1.0413332887822182], [0.4340747633053713, 1.0248020469426704], [0.5180659200853382, 0.9941894832935652], [0.5905636066620379, 0.949568636289012], [0.6483867917457562, 0.8895699510264967], [0.6935845784738898, 0.8206748353165457], [0.7288984252469625, 0.7487434227072808], [0.7554424386870936, 0.6755670567849011]]

我该如何处理这个铸造问题?

【问题讨论】:

  • 试试json['jaw'] as List&lt;List&lt;double&gt;&gt;
  • 或将下巴设为final List jaw 而不是具体的。
  • 您的建议无效
  • json['jaw'].cast>() 应该可以解决问题

标签: json casting dart flutter


【解决方案1】:

来自dynamic 的嵌套集合泛型并不容易。不幸的是,如果元素是集合,则转换需要一直在最低级别进行。

取一个dynamic,你知道它是一个List,只包含Lists,只包含doubles,(即List&lt;List&lt;double&gt;&gt;,并让Dart运行时类型系统相信这样你会想要类似的东西:

List<List<T>> _castNestedList<T>(dynamic l) =>
    (l as List).map((inner) => List<T>.from(inner)).toList();

List&lt;T&gt;.from 正在获取每个内部列表并将其复制到具有正确具体化类型的新列表。如果列表中有多次读取,这比List.cast 好,因为在List.cast 的情况下,每个元素访问都有运行时类型检查。

处理这种类型的转换是 Dart 2 的一个已知痛点。从长远来看,我们希望提高可用性。参见https://github.com/leafpetersen/cast/issues/1的讨论

【讨论】:

    【解决方案2】:

    首先你需要获取一个 List 的引用,然后进行转换:

    lip: (json['lip'] as List).cast<List<double>>()
    

    或者是空感知:

    lip: (json['lip'] as List)?.cast<List<double>>()
    

    【讨论】:

    • 这不起作用的原因是 inner 列表仍然是 List&lt;dynamic&gt;。您已将cast 添加到外部列表中,但是当读取一个元素时,将进行运行时类型检查以确保该元素的具体类型为List&lt;double&gt;,但事实并非如此。您需要在元素上 map 以确保每个元素都获得正确的具体类型。
    猜你喜欢
    • 2022-11-02
    • 2020-12-25
    • 2015-02-12
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多