【发布时间】:2021-10-22 18:02:40
【问题描述】:
iam 将 Riverpod 与 dartz 一起使用,nad iam 面临一个问题,即在将未来的提供程序与我的函数一起使用时,我也无法使用其中的任何一个,我怎样才能将我想要从函数中检索到的内容与错误隔离开来处理!
我的供应商代码:
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.
【问题讨论】:
-
final activeCourseProvider = FutureProvider.autoDispose.family
, int> -
@croxx5f 那行不通!
标签: flutter dart riverpod dartz