首先,我使用的是 getx 狀態管理。從 API 接收資料時可能會有延遲。所以我嘗試使用回圈進度指示器但出錯了。
error: Conditions must have a static type of 'bool'. (non_bool_condition at [app] lib\screens\app\app.dart:56)
import 'package:get/get.dart';
class LoadingController extends GetxController {
late RxBool isLoading;
void getLoading() {
isLoading = true.obs;
Future.delayed(const Duration(seconds: 2), () {
isLoading = false.obs;
});
}
@override
void onInit() {
getLoading();
super.onInit();
}
}
所有頁面都由無狀態小部件組成
LoadingController loading = Get.put(LoadingController());
child: loading.isLoading
? Center(
child: CircularProgressIndicator(),
)
: Container(
decoration: BoxDecoration(
color: cCardColor,
borderRadius: BorderRadius.circular(8.r),
),
我不想讓頁面有狀態,但我找不到方法
uj5u.com熱心網友回復:
發生此錯誤是因為您定義了一個可觀察變數,但像普通變數一樣使用并為其賦值
您使用了一個Observable變數,因此您必須使用ObxorGetX小部件來重建 UI。
像這樣更改 UI 代碼:
LoadingController loading = Get.put(LoadingController());
child: Obx(()=>loading.isLoading.value
? Center(
child: CircularProgressIndicator(),
)
: Container(
decoration: BoxDecoration(
color: cCardColor,
borderRadius: BorderRadius.circular(8.r),
),
),
此外,控制器中存在問題。請檢查以下代碼。
import 'package:get/get.dart';
class LoadingController extends GetxController {
RxBool isLoading = false.obs;
void getLoading() {
isLoading(true);
Future.delayed(const Duration(seconds: 2), () {
isLoading(false);
});
}
@override
void onInit() {
getLoading();
super.onInit();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/426602.html
