首页 > 解决方案 > 没有缓存值时如何抛出 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>

任何帮助表示赞赏。谢谢你!

标签: exceptioncachingflutterdart

解决方案


推荐阅读