exception - 没有缓存值时如何抛出 CacheException
问题描述
如果没有任何可用的 json(即没有互联网连接),我在尝试抛出 cacheException 时遇到问题。我正在使用 Flutter,并且正在对该功能进行测试。我的代码块看起来像这样。
Future<PostModel> getLastPost() {
final jsonString = sharedPreferences.getString(CACHED_POST);
if (jsonString != null) {
return Future.value(PostModel.fromJson(json.decode(jsonString)));
} else {
/*The error is thrown here->*/ throw CacheException();
}
}
颤振给我这个错误
Expected: throws <Instance of 'TypeMatcher<CacheException>'>
Actual: <Closure: () => Future<PostModel>>
Which: threw <Instance of 'CacheException'>
stack package:lookr_flutter/features/social/data/datasources/social_local_data_source.dart 26:7 SocialLocalDataSourceImpl.getLastPost
features/social/data/datasources/social_local_data_source_test.dart 46:26 main.<fn>.<fn>.<fn>
package:test_api expect
package:flutter_test/src/widget_tester.dart 229:3 expect
features/social/data/datasources/social_local_data_source_test.dart 46:9 main.<fn>.<fn>
任何帮助表示赞赏。谢谢你!
解决方案
推荐阅读
- apache-spark - Spark 应用程序因错误“Signal Term”而终止
- swift - 在 Swift 中将文本限制为一定数量的单词
- path-finding - 从 Godot 中的 Curve3D (Path) 获取手动定义的向量
- ios - iOS 存储 SQLite 数据库
- c# - gRPC 间歇性地具有高延迟
- javascript - 选项卡:数据切换不展开数据
- android - Wifi状态不会在android中改变?
- keyboard - Android WebVIew中的键盘长键按下
- django - JSONField order_by 查询名称中带有标点的字段的问题
- c# - C# 无法使用可观察集合绑定到列表视图