What is GetX in flutter ? how to use GetX in our flutter projects ?

We know that GetX is a Flutter package that provides a simple and efficient way to manage the state of your flutter application. It is based on the Model-Update-View architecture and provides a number of benefits over other state management solutions, including a simplified syntax, automatic reactive updates, and easy access to state from anywhere in your application.


Example of how to implement GetX in a Flutter application:

Step 1: Add the GetX package to your project's pubspec.yaml file:

dependencies:
  get: ^2.2.0


Step 2: Create a model class that will hold your state data. For example:

class CounterModel extends GetxController {
  int count = 0;

  void increment() {
    count++;
    update();
  }
}


Step 3: Use the GetX widget in your view to bind to the model's state and call its methods.

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GetX<CounterModel>(
        init: CounterModel(),
        builder: (model) => Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'You have pushed the button this many times:',
              ),
              Text(
                '${model.count}',
                style: Theme.of(context).textTheme.headline4,
              ),
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => Get.find<CounterModel>().increment(),
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}


Above example, the GetX widget is used to bind to the CounterModel instance, and its builder property is used to render the view. The view updates automatically whenever the model's state changes and the increment method is called when the floating action button is pressed

This is a basic example of how to implement GetX in a Flutter application, but we can use it to build more complex state management solutions as well.

What are 'obs' and 'Obx' with an example?


Obs and Obx are two key concepts in GetX, a state management library for Flutter. 

  • Obs: The obs keyword is used in GetX to declare an observable property in a model. An observable property is a property that can be monitored for changes and triggers an automatic update to the view whenever its value changes. This makes it easy to keep the view in sync with the state of your application.
class CounterModel extends GetxController {
  int _count = 0;
  int get count => _count;
  set count(int value) {
    _count = value;
    update();
  }
}

  • Obx: The Obx widget is used in GetX to render a view that is reactive to changes in the state of your application. The Obx widget takes a watch property, which is a list of observables to watch for changes. When one of the observables changes, the Obx widget will automatically update its view.
class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Obx(() => Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '${Get.find<CounterModel>().count}',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      )),
      floatingActionButton: FloatingActionButton(
        onPressed: () => Get.find<CounterModel>().count++,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
The Obx widget allows you to build reactive views that automatically update when the state of your application changes. This helps simplify your code and reduce the amount of boilerplate required to manage the state of your application.

Comments

Popular posts from this blog

Error Handling in Flutter - Gradle issue

How to Make a Dynamic and Trending ListView with Flutter Widgets?

Understanding API integration with Getx State management