how to Use class as datatype in dart Parse Json

Viewed 2286

I have Main class this is called FullTrip this class has attributes called itineraries and others contain two pieces of data
so i create another class called Hc now i need to use this combination two parse json response from the server

class FullTrip extends Trip{

  final List<String> including;
  final List<String> excluding;
  final List<Hc> itineraries;
  final List<Hc> policies;

  FullTrip(this.including,this.excluding,this.itineraries,this.policies,int id,String title,double price,String overview,String hero_image): 
  super(id:id,title:title,price:price,overview:overview,hero_image:hero_image);


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

  class Hc {
    final String head;
    final String content;
    Hc({this.head,this.content});
  }

when i use the code like that and run serialization command

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

i got error in terminal

[SEVERE] json_serializable on lib/models/fulltrip.dart: Error running JsonSerializableGenerator Could not generate fromJson code for itineraries because of type Hc. None of the provided TypeHelper instances support the defined type. package:Tourism/models/fulltrip.dart:21:18 final List itineraries; ^^^^^^^^^^^ [WARNING] json_serializable on lib/models/fulltrip.dart: Missing "part 'fulltrip.g.dart';". [INFO] Running build completed, took 3.0s

[INFO] Caching finalized dependency graph... [INFO] Caching finalized dependency graph completed, took 68ms

1 Answers

The problem seems to be that you have to split both classes in seperate dart files. Here is how the content of both dart files should look :

FullTrip.dart

import 'package:json_annotation/json_annotation.dart';

part 'fulltrip.g.dart';

@JsonSerializable()
class FullTrip extends Trip{

  final List<String> including;
  final List<String> excluding;
  final List<Hc> itineraries;
  final List<Hc> policies;

  FullTrip(this.including,this.excluding,this.itineraries,this.policies,int id,String title,double price,String overview,String hero_image): 
  super(id:id,title:title,price:price,overview:overview,hero_image:hero_image);


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

  Map<String, dynamic> toJson() => _$FullTripToJson(this);
}

Hc.dart

import 'package:json_annotation/json_annotation.dart';

part 'hc.g.dart';

@JsonSerializable()
class Hc {
  final String head;
  final String content;
  Hc({this.head,this.content});

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

  Map<String, dynamic> toJson() => _$HcToJson(this);
}

You should also take a look at the warning : [WARNING] json_serializable on lib/models/fulltrip.dart: Missing "part 'fulltrip.g.dart';"

You always have to add the "part" file on top of the class.

Take a look at the official documentation on how it should look : https://flutter.io/docs/development/data-and-backend/json#creating-model-classes-the-json_serializable-way

Related