【发布时间】:2021-07-30 20:59:09
【问题描述】:
我正在使用freezed.
我的代码如下所示:
import 'package:freezed_annotation/freezed_annotation.dart';
part 'key_state.freezed.dart';
part 'key_state.g.dart';
@freezed
class KeyState with _$KeyState {
factory KeyState({
CancelToken? token,
// ...
}) = _KeyState;
factory KeyState.initial() => KeyState();
factory KeyState.fromJson(Map<String, dynamic> json) =>
_$KeyStateFromJson(json);
}
这是来自https://github.com/flutterchina/dio/blob/master/dio/lib/src/cancel_token.dart 的CancelToken 类
这不起作用。
错误:
Could not generate `fromJson` code for `token`.
To support the type `CancelToken` you can:
* Use `JsonConverter`
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html
* Use `JsonKey` fields `fromJson` and `toJson`
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html
package:flutter_app/redux/state/key_state.freezed.dart:114:22
我该怎么做?
【问题讨论】:
-
可以分享
CancelToken模型类 -
@YeasinSheikh 它来自 Dio 库 pub.dev/packages/dio#cancellation
-
@YeasinSheikh github.com/flutterchina/dio/blob/master/dio/lib/src/…
-
查看How to Ask 来改进这个问题