Ternary operator not changing state

158

You need to move your Obx widget up and wrap the Visibility widget with it:

Obx(()=> Visibility(....));

Update

You have problem with this line:

todos = storedTodos.map((e) => Todo.fromJson(e)).toList().obs;

Instead of re-assigning the Rx variable itself, you should assign the data to the Rx variables value:

todos.assignAll(storedTodos.map((e) => Todo.fromJson(e)).toList());
Share:
158
Rohith Nambiar
Author by

Rohith Nambiar

Student, Geek, Love Python and Flutter

Updated on December 23, 2022

Comments

  • Rohith Nambiar
    Rohith Nambiar over 1 year

    There is a text displaying the scheduled time, Basically, the color of the text should change depending on the time, so if the scheduled time is in the future then it should not change its color and if it's in the past then its color should change to red. The scheduled time is when the notification should appear and when the notification appears it should change, the ternary operation works properly but only when I click another widget to change its state, it does not change the state on its own. I am using getx so I also tried Obx but that didn't work. How can I resolve this?

                     Visibility(
                                visible: todoController.todos[index].date ==
                                            '' &&
                                        todoController.todos[index].time == ''
                                    ? false
                                    : true,
                                child: Obx(() => Text(
                                    (todoController.todos[index].date != now)
                                        ? '${todoController.todos[index].date!}, ${todoController.todos[index].time}'
                                        : 'Today, ${todoController.todos[index].time}',
                                    style: GoogleFonts.notoSans(
                                      color: (run(
                                                      todoController
                                                          .todos[index].date,
                                                      todoController
                                                          .todos[index].time)
                                                  .compareTo(tz.TZDateTime.now(
                                                      tz.local)) >
                                              0)
                                          ? Theme.of(context).hintColor
                                          : Colors.redAccent,
                                      fontSize: 20.0,
                                      decoration:
                                          (todoController.todos[index].done)
                                              ? TextDecoration.lineThrough
                                              : TextDecoration.none,
                                    ))),
                              )
    

    TodoController:

    class TodoController extends GetxController {
     var todos = <Todo>[].obs;
    
     @override
     void onInit() {
      List? storedTodos = GetStorage().read<List>('todos');
    
    if (storedTodos != null) {
      todos = storedTodos.map((e) => Todo.fromJson(e)).toList().obs;
    }
    ever(todos, (_) {
      GetStorage().write('todos', todos.toList());
    });
    super.onInit();
    }}
    
    • Faiz Shahid
      Faiz Shahid over 2 years
      Please provide controller code also for better understanding
    • Rohith Nambiar
      Rohith Nambiar over 2 years
      I am using getx
    • John Joe
      John Joe over 2 years
      did you wrap your widget with Obx?
    • Rohith Nambiar
      Rohith Nambiar over 2 years
      Yes but that didn't work
    • John Joe
      John Joe over 2 years
      Show us how you wrapped your widget with Obx.
  • Rohith Nambiar
    Rohith Nambiar over 2 years
    Still doesn’t work
  • S. M. JAHANGIR
    S. M. JAHANGIR over 2 years
    Verify that your ternary produces expected results.
  • Rohith Nambiar
    Rohith Nambiar over 2 years
    It works as expected, but the only problem is it does not change state, Only when I press another widget to change the state, it changes its state
  • S. M. JAHANGIR
    S. M. JAHANGIR over 2 years
    Got it! Check the updated answer.