SoFunction
Updated on 2025-04-07

Detailed interpretation of Flutter GetX usage examples

Flutter GetX

It is a state management and dependency injection library based on the Flutter framework. It has the following advantages over other state management libraries:

  • Easy to use: FlutterGetX uses a simple and clear API design, which is easy to learn and use.
  • High Performance: FlutterGetX's state updates are written in the native Dart language and do not require a reflection mechanism, so they have advantages in performance.
  • Dependency Injection: FlutterGetX provides easy-to-use dependency injection functionality to easily manage dependencies in applications.

Use FlutterGetX for state management and dependency injection in Flutter

Here is a simple example code showing how to use FlutterGetX in Flutter for state management and dependency injection.

First, inAdd the FlutterGetX library to the file:

dependencies:
  flutter_getx: ^4.6.1

Then, in your Flutter application, introduce the FlutterGetX library and create a controller class (Controller) that inherits the GetxController class.

import 'package:get/';
class MyController extends GetxController {
  var count = 0;
  void increment() {
    count++;
    update(); // Notify Flutter hierarchy update UI  }
}

In the application, you can use GetX Widget to display the status in the controller and use GetX binding to implement dependency injection.

import 'package:flutter/';
import 'package:get/';
class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("FlutterGetX Demo"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: ,
          children: <Widget>[
            GetX<MyController>(
              // Bind MyController instance              builder: (controller) {
                return Text(
                  'Count: ${}',
                  style: (context).textTheme.headline4,
                );
              },
            ),
            SizedBox(height: 16),
            ElevatedButton(
              onPressed: () {
                // Get the MyController instance and call the increment method                <MyController>().increment();
              },
              child: Text('Increment'),
            ),
          ],
        ),
      ),
    );
  }
}

Used here()Method gets the controller instance and callsincrement()Method to update the state in the controller. In Flutter, since StatefulWidgets need to manually manage states in their subtrees, we need to use them in the controllerupdate()Method Notification Flutter hierarchy updates the UI.

FlutterGetX uses Obx to implement state management

Here is a sample code that demonstrates how to implement state management using Obx using FlutterGetX:

import 'package:flutter/';
import 'package:get/';
class MyController extends GetxController {
  var count = ;
  void increment() {
    ++;
    update();
  }
  void decrement() {
    --;
    update();
  }
}
class CounterPage extends StatelessWidget {
  final MyController controller = (MyController());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Counter')),
      body: Center(
        child: Column(
          mainAxisAlignment: ,
          children: [
            Text(
              'Count',
              style: TextStyle(fontSize: 24),
            ),
            Obx(() => Text(
                  '${}',
                  style: TextStyle(fontSize: 48),
                )),
            SizedBox(height: 16),
            ElevatedButton(
              onPressed: () => (),
              child: Text('Increment'),
            ),
            SizedBox(height: 16),
            ElevatedButton(
              onPressed: () => (),
              child: Text('Decrement'),
            ),
          ],
        ),
      ),
    );
  }
}

Dependency Injection: FlutterGetX provides a simple and powerful dependency injection system to manage dependencies in applications. You can use()Method registers instances into a dependency injection container and passes when needed()Method to get it.

FlutterGetX implements dependency injection

Here is a sample code that demonstrates how to implement dependency injection using FlutterGetX:

import 'package:flutter/';
import 'package:get/';
class MyApi {
  void fetchData() {
    print('Fetching data from API...');
  }
}
class MyService {
  final MyApi api = ();
  void doSomething() {
    ();
  }
}
class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    (MyApi());
    (MyService());
    return Scaffold(
      appBar: AppBar(title: Text('Home')),
      body: Center(
        child: ElevatedButton(
          onPressed: () => <MyService>().doSomething(),
          child: Text('Do Something'),
        ),
      ),
    );
  }
}

Routing Management: FlutterGetX provides an easy and powerful way to manage routing of applications. You can use()and()Methods to easily navigate to a new page and useProperties facilitate the acquisition of routing parameters.

FlutterGetX implements routing management

Here is a sample code that demonstrates how to implement routing management using FlutterGetX:

import 'package:flutter/';
import 'package:get/';
class DetailPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final String id = ['id'];
    return Scaffold(
      appBar: AppBar(title: Text('Detail')),
      body: Center(
        child: Text('ID: $id', style: TextStyle(fontSize: 24)),
      ),
    );
  }
}
class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Home')),
      body: Center(
        child: ElevatedButton(
          onPressed: () => (() => DetailPage(), arguments: {'id': '123'}),
          child: Text('Go to Detail'),
        ),
      ),
    );
  }
}

In short, FlutterGetX provides a range of powerful features that can help you write high-quality Flutter applications more easily.

The above is the detailed explanation of the use of Flutter GetX. For more information on the use of Flutter GetX, please pay attention to my other related articles!