ccd
ccd

Reputation: 6938

How to let if statement observable

The Obx can be used to observe widget when any related item change, but if the related item change before return widget (like the if statement), how should set this properly?

  @override
  Widget build(BuildContext context) {
    Obx(
      if (...) {
        ...
      }
    );
    return ...
 }

Upvotes: 1

Views: 1234

Answers (2)

Ahmed Osama
Ahmed Osama

Reputation: 21

This works with me with multiple condition:

body: Obx(() {
      if (controller.loadingState.value == LoadingState.loading) {
        return const CircularProgressIndicator(
          color: Colors.red,
        );
      } else if (controller.loadingState.value == LoadingState.loading) {
        return Center();
      } else {
        return Center();
      }
    })

enter image description here

Upvotes: 2

Sumed
Sumed

Reputation: 337

You cant make if statement observable because if statement cant returns the value/ widget. so if you want to use obx use ternary operator because it returns the value and works same as if condition.

Upvotes: 0

Related Questions