GETX Error: Conditions must have a static type of 'bool'
592
This error happened because you defined an observable variable but used and assigned value to it like a normal variable
You used an Observable
variable so you have to use the Obx
or GetX
widget to rebuild UI.
Change UI codes like this :
LoadingController loading = Get.put(LoadingController());
child: Obx(()=>loading.isLoading.value
? Center(
child: CircularProgressIndicator(),
)
: Container(
decoration: BoxDecoration(
color: cCardColor,
borderRadius: BorderRadius.circular(8.r),
),
),
Also, there is an issue in the controller. Please check the below code.
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();
}
}
Author by
regenin
Updated on December 19, 2022Comments
-
regenin over 1 year
Firstly, I am using getx state management. There may be delays while receiving data from the API. So I tried to use circular progress indicator but got error.
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(); } }
All pages consist of stateless widgets
LoadingController loading = Get.put(LoadingController()); child: loading.isLoading ? Center( child: CircularProgressIndicator(), ) : Container( decoration: BoxDecoration( color: cCardColor, borderRadius: BorderRadius.circular(8.r), ),
I don't want to turn the pages statefull but I couldn't find a way
-
regenin about 2 yearsthank you for help but i don't understand. what is difference between isLoading = true.obs; and isLoading(true); Actually I have not come across it used in this way generally.
-
mahmoud_eslami about 2 yearsRegarding the
get x
document we can update the observable value withvariable_name(new_value);
. I don't know exactly but I think there is another method callvariable_name.update()
. So when you assign a new value likeisLoading = true.obs;
the observable variable doesn't update.