为什么要列出<dynamic>而不是 List<Map<String,dynamic>>? [英] Why List<dynamic> and not List<Map<String,dynamic>>?

查看:29
本文介绍了为什么要列出<dynamic>而不是 List<Map<String,dynamic>>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序,可以用这种结构操作一些 JSON 数据:

I have a small program that manipulates some JSON data with this structure:

{
    "categories": [{
        "id": 0,
        "title": "cat1"
    }],
    "drinks": [{
        "categoryId": 0,
        "image": "image",
        "title": "title",
        "text": "text"
    }]

}

如果我使用动态类型或 var 关键字进行编码,一切都很好,但是在声明类型时出现以下错误.

Everything works well if I code using dynamic types or the var keyword but I am getting the following error when I am declaring types.

'列表<动态>'不是List" 类型的子类型.

这是那个程序:

import 'dart:convert';

main(List<String> args) {
  String source = ''' {
    "categories": [{
      "id": 0,
      "title": "cat1"
    }],
    "drinks": [{
      "categoryId": 0,
      "image": "image",
      "title": "title",
      "text": "text"
    }]
  }''';

  var json = jsonDecode(source);

  // Dynamic types, works ok!
  var categories = json['categories'];
  var title = categories[0]['title'];
  var id = categories[0]['id'];
  print('$title with $id');


  // Explicit types, error!
  List<Map<String,dynamic>> categories2 =  json['categories'];
  String title2 = categories2[0]['title'];
  int id2 = categories2[0]['id'];
  print('$title2 with $id2');

}

为什么我会收到这个错误?categoriesList 类型,但为什么它不是 List> ?

Why I am getting this error? categories is of List<dynamic> type but why it's not a List<Map<String,dynamic>>?

推荐答案

在 Dart 中,列表的元素类型在创建列表时提供,并作为列表值的一部分保留.您向列表中添加元素的每种类型都会被检查为该类型.这与在运行时擦除泛型类型参数的 Java 不同.

In Dart, the element type of a list is provided when the list is created, and it is kept as part of the list value. Every type you add an element to the list, it is checked to be of that type. This is unlike Java where generic type arguments are erased at runtime.

这意味着 Dart ListList 不同,即使两个列表都只包含实例Map>.例如,您可以向前者添加一个整数,而不能向后者添加一个整数.

That means that a Dart List<dynamic> is not the same as a List<Map<String, dynamic>>, even if both lists contains only instances of Map<String, dynamic>>. For example, you can add an integer to the former and not to the latter.

JSON 列表可以包含任何 JSON 值.在这种情况下,您的列表仅包含地图值,但这可能是巧合.JSON 解析器不会首先检查源代码以查看列表是否仅包含一种对象,而是仅创建一个 List 并将元素放入其中.创建的列表是可变的,因此有人可能会在返回后向该列表添加一个整数.如果解析器将列表设为 List>,那么该代码就会中断.

A JSON list can contain any JSON value. In this case, your list contains only values that are maps, but that could be a coincidence. The JSON parser does not check the source code first to see if the list contains only one kind of objects, instead it just creates a List<dynamic> and puts the elements into that. The created list is mutable, so someone might add an integer to that list after it has been returned. That code would break if the parser made the list a List<Map<String, dynamic>> instead.

所以,你的列表是一个 List 因为所有由 jsonParse 创建的 JSON 列表都是......否则.

So, your list is a List<dynamic> because all JSON lists created by jsonParse are that ... and it would be a breaking change to make them anything else.

这篇关于为什么要列出&lt;dynamic&gt;而不是 List&lt;Map&lt;String,dynamic&gt;&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆