-1

私はdartzでriverpodを使用していますが、私の関数で将来のプロバイダーを使用するときに、どちらにも手が届かないという問題に直面しています.エラー処理で関数から取得したいものをどのように分離できますか!

私のプロバイダーコード:

final activeCourseProvider =
    FutureProvider.autoDispose.family<List<CourseModel>, int>((ref, yearId) {
  final _courseRepository = ref.watch(coursesRepositoryProvider);
  return _courseRepository.activeCourses(yearId);
});

私の機能コード:

Future<Either<ApiFailures, List<CourseModel>>> activeCourses(int yearId) async {
   try{ final response = await http.post(
        Uri.parse("http://msc-mu.com/api_verfication.php"),
        body: {"flag": "selectcourses", "year": "$yearId"});
if (response.statusCode == 200) {
        var l = json.decode(response.body) as List<dynamic>;
        var courses = l.map((e) => CourseModel.fromJson(e)).toList();
        return right(courses);
      } else {
        return left(ApiFailures.notFound());
      }
    } on SocketException {
      return left(ApiFailures.noConnection());
    } on HttpException {
      return left(ApiFailures.notFound());
    }
  }

ポップアップするエラーは次のとおりです。The return type 'Future<Either<ApiFailures, List<CourseModel>>>' isn't a 'Future<List<CourseModel>>', as required by the closure's context.

4

2 に答える 2

1

あなたのプロバイダーはではなくactiveCourseProviderを返すことになっているようです。List<CourseModel>Either<ApiFailures, List<CourseModel>>

fold次のようにEither値を使用できます。

final activeCourseProvider = FutureProvider.autoDispose.family<List<CourseModel>, int>((ref, yearId) {
  final _courseRepository = ref.watch(coursesRepositoryProvider);
  return _courseRepository.fold<List<CourseModel>>(
    (ApiFailures failure) => {
      // Handle failure
      return [];
    },
    (List<CourseModel> r) => r
  );
});

Either<ApiFailures, List<CourseModel>>ただし、プロバイダがの代わりに値を返すようにしたい場合もありますList<CourseModel>ApiFailuresこれは、プレゼンテーション層のさらに下を処理したい場合に役立ちます。これは、アーキテクチャによって異なります。

于 2021-08-21T21:14:35.267 に答える