我使用了Freezed包,需要生成json。但是,从API获取响应时会遇到错误type 'String' is not a subtype of type 'int' in type cast
。知道此错误的原因是我从API字符串获得响应,但我在模型中输入了整数
搜索固定的解决方案后,我发现可以从issue解决此问题。但是在构建应用程序时出现此错误:
Launching lib\main.dart on Redmi Note 4 in debug mode...
Compiler message:
lib/src/network/models/utang/utang_model.freezed.dart:223:20: Error: Getter not found: '_stringToInt'.
@JsonKey(fromJson: _stringToInt, toJson: _stringFromInt)
^^^^^^^^^^^^
lib/src/network/models/utang/utang_model.freezed.dart:223:42: Error: Getter not found: '_stringFromInt'.
@JsonKey(fromJson: _stringToInt, toJson: _stringFromInt)
^^^^^^^^^^^^^^
Compiler message:
lib/src/network/models/utang/utang_model.freezed.dart:223:20: Error: Getter not found: '_stringToInt'.
@JsonKey(fromJson: _stringToInt, toJson: _stringFromInt)
^^^^^^^^^^^^
lib/src/network/models/utang/utang_model.freezed.dart:223:42: Error: Getter not found: '_stringFromInt'.
@JsonKey(fromJson: _stringToInt, toJson: _stringFromInt)
^^^^^^^^^^^^^^
Target kernel_snapshot failed: Exception: Errors during snapshot creation: null
build failed.
FAILURE: Build failed with an exception.
* Where:
Script 'C:\flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 882
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\flutter\flutter\bin\flutter.bat'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 23s
Exception: Gradle task assembleDebug failed with exit code 1
Exited (sigterm)
@freezed
abstract class UtangModel with _$UtangModel {
@JsonSerializable(fieldRename: FieldRename.snake)
@JsonKey(fromJson: _stringToInt, toJson: _stringFromInt)
const factory UtangModel({
String idUtang,
UserGoogleModel pembertang,
UserGoogleModel pengutang,
int totalUtang,
int sisaUtang,
DateTime tglKembali,
String status,
String keterangan,
String selfie,
String ttd,
DateTime createdDate,
}) = _UtangModel;
factory UtangModel.fromJson(Map<String, dynamic> json) => _$UtangModelFromJson(json);
static int _stringToInt(String number) => number == null ? null : int.parse(number);
static String _stringFromInt(int number) => number?.toString();
}
我错过了什么吗?
答案 0 :(得分:2)
您必须指出此函数来自抽象类UtangModel:
@freezed
abstract class UtangModel with _$UtangModel {
@JsonSerializable(fieldRename: FieldRename.snake)
const factory UtangModel({
String idUtang,
UserGoogleModel pembertang,
UserGoogleModel pengutang,
@JsonKey(fromJson: UtangModel._stringToInt, toJson: UtangModel._stringFromInt)int totalUtang,
int sisaUtang,
DateTime tglKembali,
String status,
String keterangan,
String selfie,
String ttd,
DateTime createdDate,
}) = _UtangModel;
factory UtangModel.fromJson(Map<String, dynamic> json) => _$UtangModelFromJson(json);
static int _stringToInt(String number) => number == null ? null : int.parse(number);
static String _stringFromInt(int number) => number?.toString();
}