Search code examples
flutterdartjson-serialization

json_serializable plugin doesn't support 'File' type?


I'm using json_serializable plugin, but it doesn't seem to work with File for images. 'myclass.g.dart' is not generated. I don't have any troubles for the other types. (https://pub.dev/packages/json_serializable/versions/0.5.4#-readme-tab-)

This is my code :

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';

part 'myclass.g.dart';

@JsonSerializable()
class MyClass {
  final String name;
  final List<File> photosFile;

  MyClass({
    @required this.name,
    @required this.photosFile,
  });


  factory MyClass.fromJson(Map<String, dynamic> json) => _$MyClassFromJson(json);
  Map<String, dynamic> toJson() => _$MyClassToJson(this);


}

And this is the error :

[SEVERE] json_serializable:json_serializable on lib/model/myclass.dart (cached):
Error running JsonSerializableGenerator
Could not generate `fromJson` code for `photosFile` because of type `File`.
None of the provided `TypeHelper` instances support the defined type.
package:mydomus_edl/model/myclass.dart:11:20
   ╷
17 │   final List<File> photosFile;
   │                    ^^^^^^^^^^
   ╵
[SEVERE] Failed after 171ms

Anyone got an idea ? Thanks !


Solution

  • There are five basic value types supported by JSON Schema:
    - string.
    - number.
    - integer.
    - boolean.
    - null.

    You should implement the conversion from String to file.