Dolphin
Dolphin

Reputation: 38607

is it possible to automatically refresh component when getx value changed flutter

I am using getx get: ^4.3.8 as the state management component in flutter, when I using this code to update the controller value:

var expand = controller.newTaskExpanded.value;
controller.newTaskExpanded(!expand);

I found the component did not refresh, then I have to invoke the getx refresh function like this:

controller.refresh();

this is the controller properties define:

class HomeController extends GetxController {
  List<Widget> widgetsList = List.empty(growable: true);
  List<Todo> tasks = List.empty(growable: true);
  var newTaskExpanded = true.obs;
  var completedTaskExpanded = false.obs;
}

is is possible to auto refresh the flutter component when controller properties value changed? should I invoke the refresh function every time change the value of getx controller?

Upvotes: 0

Views: 2535

Answers (3)

krishnaacharyaa
krishnaacharyaa

Reputation: 24912

Reactive programming with Get is as easy as using setState.

Let's imagine that you have a name variable and want that every time you change it, all widgets that use it are automatically changed.

This is your count variable:

var name = 'Jonatas Borges';

To make it observable, you just need to add ".obs" to the end of it:

var name = 'Jonatas Borges'.obs;

And in the UI, when you want to show that value and update the screen whenever the values changes, simply do this:

Obx(() => Text("${controller.name}"));

For more , refer: https://pub.dev/packages/get/example

Upvotes: 3

Sanjay Sanjel
Sanjay Sanjel

Reputation: 41

As @Muhammad Sherad explained, your widget code should be wrapped with Obx() so that you widget can look for variable change and re-render the widget.

Upvotes: 2

Muhammad Sheraz
Muhammad Sheraz

Reputation: 191

wrap you UI with Obx builder like

appBar: AppBar(title: Obx(() => Text("Clicks: ${c.count}"))),

it will auto refresh the UI on value change if you want to refresh the UI manually then use GetBuilder and when you need to update the UI just call controller.update();

Upvotes: 2

Related Questions