【问题标题】:json_serializable plugin doesn't support 'File' type?json_serializable 插件不支持“文件”类型?
【发布时间】:2019-10-25 17:58:43
【问题描述】:

我正在使用 json_serializable 插件,但它似乎不适用于图像文件。未生成“myclass.g.dart”。我对其他类型没有任何麻烦。 (https://pub.dev/packages/json_serializable/versions/0.5.4#-readme-tab-)

这是我的代码:

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);


}

这是错误:

[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

有人有想法吗?谢谢!

【问题讨论】:

    标签: flutter dart json-serialization


    【解决方案1】:

    JSON Schema 支持五种基本的值类型:
    - 字符串。
    - 号码。
    - 整数。
    - 布尔值。
    - 空值。

    你应该实现从字符串到文件的转换。

    【讨论】:

    • 谢谢!这就是我所做的,我用 List 替换了 List,只存储了 File 的路径
    • 它也会这样做......祝你好运!
    猜你喜欢
    • 2011-06-21
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 2022-01-09
    • 2020-08-11
    • 1970-01-01
    • 2011-01-31
    • 2017-04-27
    相关资源
    最近更新 更多