Flutter: преобразовать список объектов json в замороженный класс

У меня такой json:

      [
    {
        "word": "hello",
        "phonetics": [
            {
                "text": "/həˈloʊ/",
                "audio": "https://lex-audio.useremarkable.com/mp3/hello_us_1_rr.mp3"
            },
            {
                "text": "/hɛˈloʊ/",
                "audio": "https://lex-audio.useremarkable.com/mp3/hello_us_2_rr.mp3"
            }
        ],
        "meanings": [
            {
                "partOfSpeech": "exclamation",
                "definitions": [
                    {
                        "definition": "Used as a greeting or to begin a phone conversation.",
                        "example": "hello there, Katie!"
                    }
                ]
            },
            {
                "partOfSpeech": "noun",
                "definitions": [
                    {
                        "definition": "An utterance of “hello”; a greeting.",
                        "example": "she was getting polite nods and hellos from people",
                        "synonyms": [
                            "greeting",
                            "welcome",
                            "salutation",
                            "saluting",
                            "hailing",
                            "address",
                            "hello",
                            "hallo"
                        ]
                    }
                ]
            },
            {
                "partOfSpeech": "intransitive verb",
                "definitions": [
                    {
                        "definition": "Say or shout “hello”; greet someone.",
                        "example": "I pressed the phone button and helloed"
                    }
                ]
            }
        ]
    }
]

Я создал замороженный класс:

      import 'package:freezed_annotation/freezed_annotation.dart';
part 'vocabularies_model.freezed.dart';
part 'vocabularies_model.g.dart';

@freezed
abstract class VocabulariesModel with _$VocabulariesModel {
  const factory VocabulariesModel({
    List<VocabularyModel>? model,
  }) = _VocabulariesModel;

  factory VocabulariesModel.fromJson(Map<String, dynamic> json) =>
      _$VocabulariesModelFromJson(json);
}

@freezed
abstract class VocabularyModel with _$VocabularyModel {
  const factory VocabularyModel(
      {String? word,
      List<Phonetics>? phonetics,
      List<Meanings>? meanings}) = _VocabularyModel;

  factory VocabularyModel.fromJson(Map<String, dynamic> json) =>
      _$VocabularyModelFromJson(json);
}

@freezed
abstract class Phonetics with _$Phonetics {
  const factory Phonetics({
    String? text,
    String? audio,
  }) = _Phonetics;
  factory Phonetics.fromJson(Map<String, dynamic> json) =>
      _$PhoneticsFromJson(json);
}

@freezed
abstract class Meanings with _$Meanings {
  const factory Meanings({
    String? partOfSpeech,
    List<Definitions>? definitions,
  }) = _Meanings;

  factory Meanings.fromJson(Map<String, dynamic> json) =>
      _$MeaningsFromJson(json);
}

@freezed
abstract class Definitions with _$Definitions {
  const factory Definitions(
      {String? definition,
      String? example,
      List<String>? synonyms}) = _Definitions;
  factory Definitions.fromJson(Map<String, dynamic> json) =>
      _$DefinitionsFromJson(json);
}

, Но при преобразовании json в класс я получил эту ошибку:

      ⛔ DioError [DioErrorType.other]: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>?'
I/flutter ( 6065): │ ⛔ #0      DioMixin.assureResponse (package:dio/src/dio_mixin.dart:832:10)
I/flutter ( 6065): │ ⛔ #1      DioMixin.fetch.<anonymous closure> (package:dio/src/dio_mixin.dart:605:14)
I/flutter ( 6065): │ ⛔ #2      _rootRunUnary (dart:async/zone.dart:1362:47)
I/flutter ( 6065): │ ⛔ #3      _CustomZone.runUnary (dart:async/zone.dart:1265:19)
I/flutter ( 6065): │ ⛔ <asynchronous suspension>
I/flutter ( 6065): │ ⛔ #4      _RestClient.getVocabulary (package:english_vocabulary_builder/app/services/rest_client.g.dart:21:21)
I/flutter ( 6065): │ ⛔ <asynchronous suspension>
I/flutter ( 6065): │ ⛔ #5      AddRemoteDataSource.getVocabulary (package:english_vocabulary_builder/feautures/home/data/data_source/add_remote_data_source.dart:12:24)
I/flutter ( 6065): │ ⛔ <asynchronous suspension>
I/flutter ( 6065): │ ⛔ #6      AddController.showAddDialog (package:english_vocabulary_builder/feautures/home/presentation/controller/add_controller.dart:26:23)
I/flutter ( 6065): │ ⛔ <asynchronous suspension>
I/flutter ( 6065): │ ⛔ 

0 ответов

Другие вопросы по тегам