Popular GetX commands in Flutter

Popular GetX Commands

In GetX, there are several commonly used commands or methods for managing state, navigation, and dependencies. Here are some of the key `Get.` commands used in GetX:

1. GetBuilder: A widget that rebuilds itself whenever the specified controller or reactive class changes state.

2. GetX: A widget that listens to changes in the specified controller or reactive class and rebuilds itself accordingly.

3. Get.put: Binds an instance of a class to a specific route, making it available for dependency injection and accessible throughout the widget tree.

4. Get.lazyPut: Lazily binds an instance of a class to a specific route, meaning the instance is created only when it is requested.

5. Get.find: Retrieves an instance of a class or service from the dependency injection container.

6. Get.create: Creates a new instance of a class or service whenever it is requested.

7. Get.putAsync: Binds an asynchronously created instance of a class to a specific route.

8. Get.lazyPutAsync: Lazily binds an asynchronously created instance of a class to a specific route.

9. Get.delete: Removes an instance of a class or service from the dependency injection container.

10. Get.off: Navigates to a new route, replacing the current route in the navigation stack.

11. Get.to: Navigates to a new route, pushing it onto the navigation stack.

12. Get.back: Navigates back to the previous route in the navigation stack.

Examples How to use

State Management (GetX Obx):

import 'package:get/get.dart';

//Controller 
class MyController extends GetxController {
  var count = 0.obs;

  void increment() {
    count.value++;
  }
}

class MyPage extends StatelessWidget {
  final MyController controller = Get.put(MyController());//Here we use controller

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('GetX State Management'),
      ),
      body: Center(
        child: Obx(() => Text('Count: ${controller.count}')),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: controller.increment,
        child: Icon(Icons.add),
      ),
    );
  }
}

Navigation (GetX Routing):

import 'package:get/get.dart';

class HomeController extends GetxController {
  void goToNextPage() {
    Get.to(NextPage());
  }
}

class HomePage extends StatelessWidget {
  final HomeController controller = Get.put(HomeController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('GetX Navigation'),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: controller.goToNextPage,
          child: Text('Go to Next Page'),
        ),
      ),
    );
  }
}

class NextPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Next Page'),
      ),
      body: Center(
        child: Text('This is the Next Page'),
      ),
    );
  }
}

Dependency Injection (GetX Dependency Management):

import 'package:get/get.dart';

class UserRepository {
  void login() {
    print('User logged in');
  }
}

class UserController extends GetxController {
  final UserRepository userRepository;

  UserController({required this.userRepository});

  void doLogin() {
    userRepository.login();
  }
}

void main() {
  final userRepository = UserRepository();
  Get.put(UserController(userRepository: userRepository));

  Get.find<UserController>().doLogin();
}

These are just some of the commonly used Get. commands in GetX. Each command serves a specific purpose in managing state, navigation, and dependency injection in Flutter applications using GetX.

Leave a Reply

Your email address will not be published. Required fields are marked *

web_horizontal
About Us ♢ Disclaimer ♢ Privacy Policy ♢ Terms & Conditions ♢ Contact Us

Copyright © 2023 ResearchThinker.com. All rights reserved.