Search code examples
flutterdartjsonconverter

how to implement JsonConverter in dart flutter?


I am trying make custom JsonConverter using JsonConverter in dart but I am not able to use it with error message saying "Classes can only extend other classes."
Do I need to import something to use JsonConverter? or what am I missing?

class MyJsonConverter extends JsonConverter<Value, JSON> {
  // TODO
}

@JsonSerializable()
@MyJsonConverter()
class Example {
  final Value property;
}

Solution

  • To use JsonConverter, you need to add the “json_annotation” library to your project, then import it into your file.

    Using Flutter

    flutter pub add json_annotation
    

    Using Dart

    dart pub add json_annotation
    

    Then, you import the library into your file :

    import 'package:json_annotation/json_annotation.dart';