Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

GETX Error: Conditions must have a static type of 'bool'

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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();
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading