Flutter 给出的 Json 序列化不能分配给类型 'Map<String, dynamic>' 的变量



我正在尝试序列化一个简单的Dart类。正如您所看到的,我添加了与序列化相关的所有内容。文件名为ParamDescription.dart

import 'package:json_annotation/json_annotation.dart';
part 'ParamDescription.g.dart';
@JsonSerializable()
class FxParamDescription {
String name;
double minValue;
double maxValue;
double defaultValue;
FxParamDescription(){
}
factory FxParamDescription.fromJson(Map<String, dynamic> json) => _$FxParamDescriptionFromJson(json);
Map<String, dynamic> toJson() => _$FxParamDescriptionFromJson(this);
}

我运行了flutter pub run build_runner build,它完成时没有出现错误。

当试图编译时,我得到了这个错误

^
lib/fx/ParamDescription.dart:24:65: Error: The argument type 'FxParamDescription' can't be assigned to the parameter type 'Map<String, dynamic>'.
- 'FxParamDescription' is from 'package:flutter_app/fx/ParamDescription.dart' ('lib/fx/ParamDescription.dart').
- 'Map' is from 'dart:core'.
Map<String, dynamic> toJson() => _$FxParamDescriptionFromJson(this);
^
lib/fx/ParamDescription.dart:24:36: Error: A value of type 'FxParamDescription' can't be assigned to a variable of type 'Map<String, dynamic>'.
- 'FxParamDescription' is from 'package:flutter_app/fx/ParamDescription.dart' ('lib/fx/ParamDescription.dart').
- 'Map' is from 'dart:core'.
Map<String, dynamic> toJson() => _$FxParamDescriptionFromJson(this);

更改

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

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

您使用了错误生成的函数。

相关内容

最新更新