gpt4 book ai didi

flutter 卡住 : equals type of object is not same

转载 作者:行者123 更新时间:2023-12-05 01:54:36 25 4
gpt4 key购买 nike

我正在使用 freezed 从 json 生成对象:

@freezed
class UserMessagesResponseModel with _$UserMessagesResponseModel {
const factory UserMessagesResponseModel({
final Data? data,
}) = _UserMessagesResponseModel;

factory UserMessagesResponseModel.fromJson(final Map<String, dynamic> json) =>
_$UserMessagesResponseModelFromJson(json);
}

@freezed
class Data with _$Data {
const factory Data({
final Messages? messages,
}) = _Data;

factory Data.fromJson(final Map<String, dynamic> json) =>
_$DataFromJson(json);
}...

现在我正在尝试对创建的对象进行测试和检查类型:

test('should return a valid model', () async {
final jsonMap =
json.decode(fixture('message.json')) as Map<String, dynamic>;

final result = UserMessagesResponseModel.fromJson(
jsonMap,
);

expect(result, equals(UserMessagesResponseModel));
});

为什么会出错:

Expected: Type:<UserMessagesResponseModel>
Actual: _$_UserMessagesResponseModel:<UserMessagesResponseModel...

冷冻 不是同款?如何检查类型?

我还用过:

expect(result, isA<UserMessagesResponseModel>);

但我明白了:

Expected: <Closure: () => TypeMatcher<UserMessagesResponseModel> from Function 'isA': static.>
Actual: _$_UserMessagesResponseModel:<UserMessagesResponseModel

最佳答案

第一个例子

expect(result, equals(UserMessagesResponseModel));

不起作用,因为结果是一个实例并且 UserMessagesResponseModel 是一种类型,您应该使用 isA

但是在第二个例子中我使用了 isA 但它仍然不起作用!

您在 isA 之后错过了 (),我相信这就是它不起作用的原因。

来自这里:

expect(result, isA<UserMessagesResponseModel>);

为此:

expect(result, isA<UserMessagesResponseModel>());

关于 flutter 卡住 : equals type of object is not same,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70587867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com