当没有缓存值时,如何抛出CacheException

当没有缓存值时,如何抛出CacheException,exception,caching,flutter,dart,Exception,Caching,Flutter,Dart,如果没有可用的json(即没有internet连接),我在尝试抛出cacheException时遇到问题。我正在使用颤振,并正在对该功能进行测试。我的代码块如下所示 Future<PostModel> getLastPost() { final jsonString = sharedPreferences.getString(CACHED_POST); if (jsonString != null) { return Future.value(Post

如果没有可用的json(即没有internet连接),我在尝试抛出cacheException时遇到问题。我正在使用颤振,并正在对该功能进行测试。我的代码块如下所示

  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();
    }
  }
Future getLastPost(){
final jsonString=SharedReferences.getString(缓存的\u POST);
if(jsonString!=null){
返回Future.value(PostModel.fromJson(json.decode(jsonString));
}否则{
/*此处抛出错误->*/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>
应为:抛出
实际:未来>
哪一个:扔
堆栈包:lookr\u flatter/features/social/data/datasources/social\u local\u data\u source.dart 26:7 SocialLocalDataSourceImpl.getLastPost
功能/社交/数据/数据源/社交\本地\数据\来源\测试。dart 46:26主。。。
包:test_api expect
包:颤振测试/src/widget测试仪。dart 229:3预期
功能/社交/数据/数据源/社交\本地\数据\来源\测试。dart 46:9主。。
感谢您的帮助。谢谢大家!