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

类型“\u InternalLinkedHashMap”不是类型转换中“List”类型的子类型

燕朝明
2023-03-14

我试图在Dart中反序列化一个非常复杂的JSON,但是我不知道出了什么问题。

当它尝试反序列化时,它向我显示了以下错误:“type'\u InternalLinkedHashMap”

有人知道发生了什么事吗?

这是我的密码:

{
  "Instytucja": [
    "TypInstytucji": "bank krajowy",
      "PodtypInstytucji": "bank centralny",
      "NrInstytucji": 101,
      "NazwaInstytucji": "Narodowy Bank Polski",
      "SymbolLiterowy": "NBP",
      "DataRozpDzial": "1945-01-15",
      "NIP": 5250008198,
      "IdentyfikatorLEI": "549300K7Z2KT76WQJD18",
      "WWW": "www.nbp.pl",
        "Jendostka": [
        {
          "NrEwidencyjny": "A000001",
          "TypJednostki": "Centrala",
          "NazwaJednostki": "Centrala",
          "SymbolLiterowy": "C-la",
          "DaneAdresowe": {
            "kraj": "POLSKA",
            "wojewodztwo": "MAZOWIECKIE",
            "powiat": "Warszawa",
            "miejscowosc": "Warszawa",
            "ulica": "ul. Świętokrzyska",
            "numerBudynku": "11/21",
            "kodPocztowy": "00-919",
            "poczta": "Warszawa",
            "nrSkrytkiPocztowej": 1011
          },
          "DataPodjeciaDzialalnosci": "1945-01-15",
          "NumerTelefonu": "22 185 10 10",
          "NumerFaksu": "22 185 23 09",
          "NumerRozliczeniowy": [
            {
              "NrRozliczeniowy": 10100000,
              "NazwaNumeru": "Centrala",
              "KodyBIC": [
                "BIC NBPLPLPWXXX",
                "BIC SEPA NBPLPLPWXXX"
              ],
              "DataWygenerowania": "1945-01-15"
            },
            {
              "NrRozliczeniowy": 10100055,
              "NazwaNumeru": "Centrala - B2B Collect",
              "KodyBIC": [
                "BIC NBPLPLPWXXX",
                "BIC SEPA NBPLPLPWXXX"
              ],
              "DataWygenerowania": "2008-04-01"
            },
            {
              "NrRozliczeniowy": 10100068,
              "NazwaNumeru": "Centrala - NBPCollect",
              "KodyBIC": [
                "BIC NBPLPLPWXXX",
                "BIC SEPA NBPLPLPWXXX"
              ],
              "DataWygenerowania": "2008-10-01"
            },
            {
              "NrRozliczeniowy": 10100071,
              "NazwaNumeru": "Centrala - B2B-2",
              "KodyBIC": [
                "BIC NBPLPLPWXXX",
                "BIC SEPA NBPLPLPWXXX"
              ],
              "DataWygenerowania": "2016-08-01"
            }
          ]
        },
  ]
 ]
}
import 'package:equatable/equatable.dart';

abstract class Bank extends Equatable {
  List<Institution> get institutions;
}

abstract class Institution extends Equatable {
  List<Unit> get units;
}

abstract class Unit extends Equatable {
  List<SortNumber> get sortNumbers;
}

abstract class SortNumber extends Equatable {
  int get sortNumber;
}

import 'package:jaki_to_bank/features/home/domain/entities/bank.dart';
import 'package:json_annotation/json_annotation.dart';

part 'bank_model.g.dart';

@JsonSerializable()
class BankModel extends Bank {
  BankModel({required this.institutions});

  factory BankModel.fromJson(Map<String, dynamic> json) => _$BankModelFromJson(json);

  @override
  @JsonKey(name: 'Instytucja')
  final List<InstitutionModel> institutions;

  @override
  List<Object?> get props => [institutions];
}

@JsonSerializable()
class InstitutionModel extends Institution {
  InstitutionModel({required this.units});

  factory InstitutionModel.fromJson(Map<String, dynamic> json) => _$InstitutionModelFromJson(json);

  @override
  @JsonKey(name: 'Jednostka')
  final List<UnitModel> units;

  @override
  List<Object?> get props => [units];
}

@JsonSerializable()
class UnitModel extends Unit {
  UnitModel({required this.sortNumbers});

  factory UnitModel.fromJson(Map<String, dynamic> json) => _$UnitModelFromJson(json);

  @override
  @JsonKey(name: 'NumerRozliczeniowy')
  final List<SortNumberModel> sortNumbers;

  @override
  List<Object?> get props => [sortNumbers];
}

@JsonSerializable()
class SortNumberModel extends SortNumber {
  SortNumberModel({required this.sortNumber});

  factory SortNumberModel.fromJson(Map<String, dynamic> json) => _$SortNumberModelFromJson(json);

  @override
  @JsonKey(name: 'NrRozliczeniowy')
  final int sortNumber;

  @override
  List<Object?> get props => [sortNumber];
}

import 'dart:convert';

import 'package:flutter/services.dart';
import 'package:jaki_to_bank/core/app/files.dart';
import 'package:jaki_to_bank/features/home/data/models/bank_model.dart';
import 'package:jaki_to_bank/features/home/domain/entities/bank.dart';

abstract class BanksDataSource {
  Future<Bank> getBanksList();
}

class BanksDataSourceImpl extends BanksDataSource {
  @override
  Future<Bank> getBanksList() async {
    final String source = Files.banks;

    final String banks = await rootBundle.loadString(source);
    final Map<String, dynamic> banksMap = jsonDecode(banks);

    return BankModel.fromJson(banksMap);
  }
}

共有2个答案

湛功
2023-03-14

尝试在您的机构模型(以及您曾经有嵌套类的地方)上添加@JsonSerializable(明确ToJson: true),然后运行:

flutter pub run build_runner build --delete-conflicting-outputs

有关更多信息,请查看颤振。开发人员:https://flutter.dev/docs/development/data-and-backend/json?fbclid=IwAR2sA4lF5zRGneFH4KAqtYLLQXKF6dJqJ8QzmuY-NuEe-hiRPDUx5eNfRa8#为嵌套类生成代码

夹谷承安
2023-03-14

您的JSON格式不正确。

{
  "Instytucja": [
    {      <-- missing this
      "TypInstytucji": "bank krajowy",
      "PodtypInstytucji": "bank centralny",
      ...
    }
  ]
}
 类似资料:
  • 当我运行我的代码时,它返回了错误:类型“Future”不是类型转换中“List”类型的子类型。我该如何修复它?它是在我从Firebase获取数据之前匹配引擎运行的?我试图将wait添加到List demoProfiles=getdata()作为wait List;但它返回错误。我真的是新手,这个问题已经困扰了我一整天。请帮帮我

  • 我有错误从标题时试图这样做 > 我创建了@JsonSerialiazable()类来处理json 然后,在调用http时。get(),我执行以下操作: map变量如下所示: 当调用$MyResponseFromJson(json)时,当它试图从我的响应中执行(json['a']as List)时,它会给出一个错误。g、 构建运行程序生成的dart文件。 我如何修复错误? 谢啦

  • 我从Firestore示例中复制了一段代码: 但是我得到了这个错误 这里出了什么问题?

  • 我是一个新手,我尝试运行一个GitHub项目,但遇到了一个错误,比如类型ListDynamic不是类型ListInt的子类型。Github链接 误差线 以上代码文件 如有任何帮助,我们将不胜感激,并提前向您表示感谢。

  • 我正在开发一个依靠API REST调用的flutter应用程序。来自API的响应有点复杂。当调用我的API(例如:api/产品)时,我可以从日志中看到响应:但是我有这个错误: 类型列表 StackTrace: [更新]:退货产品响应。fromJson(response)而不是response ProductsRespository: ProductsBloc: 回应: ApiProvider: 模

  • 我是个新手。当我运行代码时,我得到一个错误:“Future”类型不是“List”类型的子类型。如果我改变屏幕,我将得到错误:NoSuchMethodError:方法“map”被调用为null。我怎样才能解决它?谢谢你帮助我。