簡體   English   中英

Riverpod:未來的供應商卡在加載中

[英]Riverpod : future provider is stuck on loading

ref.readref.watch執行提供程序時描述錯誤,結果是一樣的,它卡在加載塊上,而在 postman 中測試 api 工作正常,有趣的是 api 調用被執行並且每當我在里面打印了一些東西,它出現在控制台中

在表示層重現

onpressed:()=>ref
                                                  .read(getPatientProvider(
                                                      r.api_token))
                                                  .when(data: (data) {
                                                data.fold(
                                                    (l) => print(
                                                        "something wrong happened"),
                                                    (r) async {
                                                  print(r.id);
                                                  print("hello");
                                                  patient.value = patient.value
                                                      .copyWith(
                                                          name: r.name,
                                                          aliid: r.id,
                                                          appointments: r
                                                              .patient_appointments,
                                                          fcmtoken: token);
                                                  ref.read(docexist(r.id)).when(
                                                      loading: () =>
                                                          print("loading"),
                                                      error: (error, _) =>
                                                          print(error),
                                                      data: (data) async {
                                                        print("heloo");
                                                        if (data.isEmpty) {
                                                          print(
                                                              "data is not empty");
                                                        } else {
                                                          return print(
                                                              "logged in normally");
                                                        }
                                                      });
                                                });
                                              }, error: (error, _) {
                                                print(error);
                                              }, loading: () {
                                                print("object");
                                              })

提供 riverpod 生成器

@riverpod
Future<Either<ApiFailures, dynamic>> getPatient(
    GetPatientRef ref, String token) async {
  final patientProvider = ref.watch(patientRepositoryProvider);
  return patientProvider.getInfo(token);
}

基礎設施層

@override
  Future<Either<ApiFailures, dynamic>> getInfo(String token) {
    var dio = Dio();
    final result = TaskEither<ApiFailures, PatientModel>(() async {
      try {
        final response = await dio.get(
            "https://xxxxxxxx/GetInfo?api_token=$token");
        if (response.data == null) {
          return const Left(ApiFailures.notfound());
        } else {
          PatientModel patientModel =
              PatientModel.fromJson(response.data["User"]);
          return Right(patientModel);
        }
      } catch (err, st) {
        final message = 'error ${err.runtimeType}]';

        if (kDebugMode) log(message, error: err, stackTrace: st);

        if (err is DioError) {
          return Left(ApiFailures.fromDioError(error: err));
        }
        return const Left(ApiFailures.internalError());
      }
    });
    return result.map((r) => r).run();
  }

它應該像往常一樣獲取數據的預期行為

在單擊處理程序(例如onPressed )中調用when是沒有意義的。

“何時”不等待未來完成。 它根據未來的當前狀態立即執行。
考慮到你調用的時候只是觸發了future,那么那個時候的future就一直在loading state中。

你想要的是像async / await這樣的東西,在那里你可以等到你的未來完成。

你可以這樣做:

onPressed: () async {
  final value = await ref.read(provider.future);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM