我正在尝试将freezed与json_serializable结合使用。我正在使用以下版本
freezed: ^0.9.2
json_serializable: ^3.2.5
freezed_annotation: ^0.7.1
我的冻结类(class)看起来像这样
import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'freezed_classes.freezed.dart';
@freezed
abstract class User with _$User {
factory User(
{@required String uid,
String firstName,
String lastName,
String email,
Map<int, String> pictures
}) = _User;
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
}
我正在使用它来生成类
flutter pub run build_runner watch --delete-conflicting-outputs
该命令似乎已成功执行,但我的IDE似乎引发了这些编译问题
error: The method '_$_$_UserFromJson' isn't defined for the class '_$_User'. (undefined_method at lib/freezed/freezed_classes.freezed.dart:68)
error: The method '_$_$_UserToJson' isn't defined for the class '_$_User'. (undefined_method at lib/freezed/freezed_classes.freezed.dart:156)
知道为什么会这样吗?
我尝试进行清除并重新启动IDE,但这仍然存在。
最佳答案
根据freezed
文档:
The changes necessary to make it compatible with json_serializable consists of two lines:
a new part: part 'model.g.dart';
a new constructor on the targeted class: factory Model.fromJson(Map<String, dynamic> json) => _$ModelFromJson(json);
您缺少
part
所需的json_serializable
:part freezed_classes.g.dart
关于flutter - Flutter:Freezed和json_serializable软件包的编译问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60691939/