当前位置: 首页 > 知识库问答 >
问题:

类型列表不是类型映射的子类型

商冠玉
2023-03-14

我对Flutter编程非常陌生,我正在尝试导入一个本地JSON文件,其中包含书籍数据,如标题、作者、发布年份等。

最初我使用JSON-to-DART转换器来组装一个图书数据模型,现在我正在尝试创建一个getBooksAll()函数,在该函数中,程序获取本地JSON文件,并使用BookData类中的fromJson()方法将数据解析为地图。

我遇到的问题是json。解码(值)返回类型列表

以下是getBooksAll()代码:

  static getBooksAll() {
    var booksJson = rootBundle.loadString('assets/bookData.json');
    booksJson.then((value) => BookData.fromJson(json.decode(value)));
  }

这是我得到的错误:

E/flutter (10608): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
E/flutter (10608): #0      BookData.getBooksAll.<anonymous closure> (package:well_red_v1/models/book_data_model.dart:56:54)
E/flutter (10608): #1      _rootRunUnary (dart:async/zone.dart:1436:47)
E/flutter (10608): #2      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter (10608): <asynchronous suspension>
E/flutter (10608): 

以下是整本书的数据模型代码:

import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:flutter/services.dart' show rootBundle;

class BookData {
  Id? id;
  String? title;
  String? isbn;
  int? pageCount;
  PublishedDate? publishedDate;
  String? thumbnailUrl;
  String? shortDescription;
  String? longDescription;
  String? status;
  List<dynamic>? authors;
  List<dynamic>? categories;

  BookData({this.id, this.title, this.isbn, this.pageCount, this.publishedDate, this.thumbnailUrl, this.shortDescription, this.longDescription, this.status, this.authors, this.categories});

  BookData.fromJson(Map<String, dynamic> json) {
    this.id = json["_id"] == null ? null : Id.fromJson(json["_id"]);
    this.title = json["title"];
    this.isbn = json["isbn"];
    this.pageCount = json["pageCount"];
    this.publishedDate = json["publishedDate"] == null ? null : PublishedDate.fromJson(json["publishedDate"]);
    this.thumbnailUrl = json["thumbnailUrl"];
    this.shortDescription = json["shortDescription"];
    this.longDescription = json["longDescription"];
    this.status = json["status"];
    this.authors = json["authors"] ?? [];
    this.categories = json["categories"] ?? [];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if(this.id != null)
      data["_id"] = this.id?.toJson();
    data["title"] = this.title;
    data["isbn"] = this.isbn;
    data["pageCount"] = this.pageCount;
    if(this.publishedDate != null)
      data["publishedDate"] = this.publishedDate?.toJson();
    data["thumbnailUrl"] = this.thumbnailUrl;
    data["shortDescription"] = this.shortDescription;
    data["longDescription"] = this.longDescription;
    data["status"] = this.status;
    if(this.authors != null)
      data["authors"] = this.authors;
    if(this.categories != null)
      data["categories"] = this.categories;
    return data;
  }

  static getBooksAll() {
    var booksJson = rootBundle.loadString('assets/bookData.json');
    booksJson.then((value) => BookData.fromJson(json.decode(value)));
  }
}

class PublishedDate {
  String? $date;

  PublishedDate({this.$date});

  PublishedDate.fromJson(Map<String, dynamic> json) {
    this.$date = json["${$date}"];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data["${$date}"] = this.$date;
    return data;
  }
}

class Id {
  String? $oid;

  Id({this.$oid});

  Id.fromJson(Map<String, dynamic> json) {
    this.$oid = json["${$oid}"];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data["${$oid}"] = this.$oid;
    return data;
  }
}

下面是JSON文件的一部分:

[
  {
    "_id": 1,
    "title": "Unlocking Android",
    "isbn": "1933988673",
    "pageCount": 416,
    "publishedDate": {
      "$date": "2009-04-01T00:00:00.000-0700"
    },
    "thumbnailUrl": "https://s3.amazonaws.com/AKIAJC5RLADLUMVRPFDQ.book-thumb-images/ableson.jpg",
    "shortDescription": "Unlocking Android: A Developer's Guide provides concise, hands-on instruction for the Android operating system and development tools. This book teaches important architectural concepts in a straightforward writing style and builds on this with practical and useful examples throughout.",
    "longDescription": "Android is an open source mobile phone platform based on the Linux operating system and developed by the Open Handset Alliance, a consortium of over 30 hardware, software and telecom companies that focus on open standards for mobile devices. Led by search giant, Google, Android is designed to deliver a better and more open and cost effective mobile experience.    Unlocking Android: A Developer's Guide provides concise, hands-on instruction for the Android operating system and development tools. This book teaches important architectural concepts in a straightforward writing style and builds on this with practical and useful examples throughout. Based on his mobile development experience and his deep knowledge of the arcane Android technical documentation, the author conveys the know-how you need to develop practical applications that build upon or replace any of Androids features, however small.    Unlocking Android: A Developer's Guide prepares the reader to embrace the platform in easy-to-understand language and builds on this foundation with re-usable Java code examples. It is ideal for corporate and hobbyists alike who have an interest, or a mandate, to deliver software functionality for cell phones.    WHAT'S INSIDE:        * Android's place in the market      * Using the Eclipse environment for Android development      * The Intents - how and why they are used      * Application classes:            o Activity            o Service            o IntentReceiver       * User interface design      * Using the ContentProvider to manage data      * Persisting data with the SQLite database      * Networking examples      * Telephony applications      * Notification methods      * OpenGL, animation & multimedia      * Sample Applications  ",
    "status": "PUBLISH",
    "authors": [
      "W. Frank Ableson",
      "Charlie Collins",
      "Robi Sen"
    ],
    "categories": [
      "Open Source",
      "Mobile"
    ]
  },
  {
    "_id": 2,
    "title": "Android in Action, Second Edition",
    "isbn": "1935182722",
    "pageCount": 592,
    "publishedDate": {
      "$date": "2011-01-14T00:00:00.000-0800"
    },
    "thumbnailUrl": "https://s3.amazonaws.com/AKIAJC5RLADLUMVRPFDQ.book-thumb-images/ableson2.jpg",
    "shortDescription": "Android in Action, Second Edition is a comprehensive tutorial for Android developers. Taking you far beyond \"Hello Android,\" this fast-paced book puts you in the driver's seat as you learn important architectural concepts and implementation strategies. You'll master the SDK, build WebKit apps using HTML 5, and even learn to extend or replace Android's built-in features by building useful and intriguing examples. ",
    "longDescription": "When it comes to mobile apps, Android can do almost anything   and with this book, so can you! Android runs on mobile devices ranging from smart phones to tablets to countless special-purpose gadgets. It's the broadest mobile platform available.    Android in Action, Second Edition is a comprehensive tutorial for Android developers. Taking you far beyond \"Hello Android,\" this fast-paced book puts you in the driver's seat as you learn important architectural concepts and implementation strategies. You'll master the SDK, build WebKit apps using HTML 5, and even learn to extend or replace Android's built-in features by building useful and intriguing examples. ",
    "status": "PUBLISH",
    "authors": [
      "W. Frank Ableson",
      "Robi Sen"
    ],
    "categories": [
      "Java"
    ]
  },
  {
    "_id": 3,
    "title": "Specification by Example",
    "isbn": "1617290084",
    "pageCount": 0,
    "publishedDate": {
      "$date": "2011-06-03T00:00:00.000-0700"
    },
    "thumbnailUrl": "https://s3.amazonaws.com/AKIAJC5RLADLUMVRPFDQ.book-thumb-images/adzic.jpg",
    "status": "PUBLISH",
    "authors": [
      "Gojko Adzic"
    ],
    "categories": [
      "Software Engineering"
    ]
  }
]

共有1个答案

刁英朗
2023-03-14

对象映射列表的解决方案:

  static getBooksAll() async {
    var bookURL =
        "https://raw.githubusercontent.com/dineshnagarajandev/samplejson/main/books.json";
    var response = await http.get(Uri.parse(bookURL));
    var listToPass = jsonDecode(response.body);
    List<BookData> bookData =
        List<BookData>.from(listToPass.map((i) => BookData.fromJson(i)));
    print(bookData);
  }

注意:Id类有类型大小写问题,需要更新。您使用的id是int,但声明String

查看解决方案注释

this.id = json["_id"] == null ? null : Id.fromJson(json["_id"]);

编辑:

您正在使用的模型没有\u id作为对象。您可以删除ID类并使用以下内容更新您的模型

class BookData {
  int? id;
  String? title;
  String? isbn;
  int? pageCount;
  PublishedDate? publishedDate;
  String? thumbnailUrl;
  String? shortDescription;
  String? longDescription;
  String? status;
  List<dynamic>? authors;
  List<dynamic>? categories;

  BookData(
      {this.id,
      this.title,
      this.isbn,
      this.pageCount,
      this.publishedDate,
      this.thumbnailUrl,
      this.shortDescription,
      this.longDescription,
      this.status,
      this.authors,
      this.categories});

  BookData.fromJson(Map<String, dynamic> json) {
    this.id = json["_id"] == null ? null : json["_id"];
    this.title = json["title"];
    this.isbn = json["isbn"];
    this.pageCount = json["pageCount"];
    this.publishedDate = json["publishedDate"] == null
        ? null
        : PublishedDate.fromJson(json["publishedDate"]);
    this.thumbnailUrl = json["thumbnailUrl"];
    this.shortDescription = json["shortDescription"];
    this.longDescription = json["longDescription"];
    this.status = json["status"];
    this.authors = json["authors"] ?? [];
    this.categories = json["categories"] ?? [];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.id != null) data["_id"] = this.id;
    data["title"] = this.title;
    data["isbn"] = this.isbn;
    data["pageCount"] = this.pageCount;
    if (this.publishedDate != null)
      data["publishedDate"] = this.publishedDate?.toJson();
    data["thumbnailUrl"] = this.thumbnailUrl;
    data["shortDescription"] = this.shortDescription;
    data["longDescription"] = this.longDescription;
    data["status"] = this.status;
    if (this.authors != null) data["authors"] = this.authors;
    if (this.categories != null) data["categories"] = this.categories;
    return data;
  }
}

class PublishedDate {
  String? $date;

  PublishedDate({this.$date});

  PublishedDate.fromJson(Map<String, dynamic> json) {
    this.$date = json["${$date}"];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data["${$date}"] = this.$date;
    return data;
  }
}
 类似资料:
  • 我是一个新的颤振和得到类型错误。我正在尝试使用json自动序列化。 在做了一些调整后这里看起来是这样的 下面是我如何尝试从api获取数据 我的类如下所示 谁能帮我一下吗。我被困在这里了。我一直在尝试不同的方法,但都不管用。非常感谢。

  • 你好,我正在做一个api调用,在那里我得到了一系列的横幅。我正在犯错误- 横幅。飞奔 横幅。飞奔 api_base_助手。飞奔 得到你的横幅。飞奔

  • 因此,我创建了一个应用程序,通过api读取数据,并尝试解析JSON api 这是我的错误截图 我试着把它改成一个列表,但它仍然读取一个错误 这是我的密码大象。飞奔 如何纠正此错误?请帮忙

  • 我在尝试使用JSON_serializable序列化从internet获取的复杂JSON并使用FutureProvider将它们连接在一起时遇到了这个问题。这是json文件, 这是包含序列化类的代码, 获取json文件并将其转换为dart的未来提供者, 这是基因编码, 这是转换后的json文件显示的代码 当我试着运行这个项目时,我遇到了这个错误 i/flutter(23347): type'Lis

  • 我试图获得用户信息后,用户输入电子邮件和密码。我使用的api会返回我用户的额外信息,所以我尝试从中获取这些信息。 我试图解析这个json: 这些是我的模型: 这是我发送电子邮件、密码和获取用户数据的部分: 这是给我错误的部分 我打印了响应体,它给出了我想要的字符串,但是jsonDecode返回了一个列表,所以我不能使用它。我怎样才能解决这个问题?提前谢谢。

  • 我目前正在构建一个通过api读取数据的应用程序,我正在尝试从JSON占位符解析JSON api。 我为用户(users_future_model.dart)做了一个模型类: 这是将json读入小部件的main.dart: 这是我试图阅读的JSON信息,尽管它只是其中的一小部分: 我当前遇到的错误是: 如何纠正此错误?