GetX Tutorial | Full GetX Tutorial

Flutter

Flutter Tutorial:

Introduction

Flutter

Why Flutter

About Flutter

Cross Platform

MVVM vs MVC vs MVP

Flutter Framework

Flutter Benefits

Flutter Comparison I

Flutter Comparison II

Flutter Comparison III

Install Flutter

Android studio vs VsCode

Android Setup

VsCode Setup

Vs Code Plugins

Android Studio Plugins

Flutter Widgets:

Flutter Basic Templates

Flutter Commands

Common Widgets

Top 10 popular widgets

Flutter Stateless vs Stateful

Type of Widgets

Flutter Text

Flutter Text Style

Textfield vs TextFormField

Flutter Scaffold

Flutter Container & SizedBox

Flutter Row & Column

Flutter Buttons

Flutter Stack

Flutter Forms

Flutter AlertDialog

Flutter Icons

Flutter Images

Flutter Drawer

Flutter ListView

Flutter GridView

Flutter Toast

Flutter Checkbox

Flutter Radio Button

Flutter Progress Bar

Flutter Tooltip

Flutter Slider

Flutter Table

Flutter SnackBar

Shimmer in Flutter

Bottom Navigation Bar

Flutter Gesture

Flutter Error Handling

Flutter DropDown

Flutter Toggle

Flutter Auto Close Keyboard

Flutter Screen Size

Flutter Advance

Custom Widget in Flutter

Flutter Navigator

Flutter Read Json

Flutter Generate Excel

Flutter Multiple Widgets

Flutter Bottom sheet

Flutter Copy to Clipboard

Flutter Tab bar

Flutter Code Editor

Flutter youtube Player

Flutter REST API

Flutter http

Flutter dio

dio vs http

Advanced Concepts

Tips Flutter App Development

Flutter App version Update

Flutter Copy Text in App

Flutter Handle Null Value

Flutter Splash Screen

Flutter Disposable

Notification Listener

Flutter Switch Cases

Flutter Slivers

Flutter Custom Appbar

Databinding in Flutter

Flutter Cards

Wrap vs Builder vs OverBarFlow

Flutter App Upgrade

GoogleMap vs FlutterMap

Circular progress contain Icon

DropDown Timer in Flutter

Flutter State management Comparison

Flutter vs Other Framework

Flutter Mixin

Flutter Database

Flutter Database

Suitable DB for Flutter

DBs for Flutter

Backend for flutter

SharedPreferences

Flutter Token Expired Handling

Flutter Provider

Flutter Provider Tutorial

Flutter GetX

Flutter GetX tutorial

Flutter with Native

Flutter FFI

Flutter Testing

Pass values in Flutter

WorkManager

Flutter Tips:

Best Practices

Reduce Flutter Screens

Tips to make app smart

Optimize App

Handle Multiple Pages

Interview Questions

Top 10 Interview Questions

Dart Interview Questions

Flutter 100 Interview Questions

Flutter 20 Interview Questions

Provider Interview Questions

GetX interview Questions

BLoC interview Questions

GetX Tutorial Part II

(GetX Tutorial Part I)

Controllers in GetX

There are three types of controllers available in GetX: GetxController, GetxService, and GetxControllerMixin

GetxController:

This is the most commonly used type of controller in GetX. It extends the Disposable Interface and provides a lifecycle management system. It is suitable for managing state within a specific scope like a widget or a screen. Example:

In this example, the CounterController extends GetxController and manages the state of a counter. It has a count variable wrapped in an observable (obs). The increment method updates the count. The onInit method is called when the controller is initialized, and the onClose method is called when the controller is closed.

import 'package:get/get.dart';

class CounterController extends GetxController {
  RxInt count = 0.obs;

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

  @override
  void onInit() {
    super.onInit();
    print('CounterController initialized');
  }

  @override
  void onClose() {
    super.onClose();
    print('CounterController closed');
  }
}

GetxService:

This type of controller is similar to GetxController, but it has a longer lifecycle. It can be accessed from anywhere in the app and is useful for managing global state or shared dependencies. Example:

In this example, the AuthService extends GetxService and manages the authentication state of the app. It has an isLoggedIn variable wrapped in an observable (obs). The login and logout methods update the authentication state. The onInit method is called when the service is initialized, and the onClose method is called when the service is closed.

import 'package:get/get.dart';

class AuthService extends GetxService {
  RxBool isLoggedIn = false.obs;

  void login() {
    isLoggedIn.value = true;
  }

  void logout() {
    isLoggedIn.value = false;
  }

  @override
  void onInit() {
    super.onInit();
    print('AuthService initialized');
  }

  @override
  void onClose() {
    super.onClose();
    print('AuthService closed');
  }
}

GetxControllerMixin:

This mixin is used when you want to combine multiple controllers into a single class. It allows you to use multiple controllers within a single class by providing a clean way to manage their lifecycle. Example:

In this example, the MixinController is a mixin that defines a mixinMethod. The MyCombinedController extends GetxController and uses the MixinController mixin. It also manages the state of a name with the name variable wrapped in an observable. The setName method updates the name. The onInit method is called when the controller is initialized, and the onClose method is called when the controller is closed.

import 'package:get/get.dart';

mixin MixinController on GetxController {
  void mixinMethod() {
    print('Mixin method called');
  }
}

class MyCombinedController extends GetxController with MixinController {
  RxString name = ''.obs;

  void setName(String newName) {
    name.value = newName;
  }

  @override
  void onInit() {
    super.onInit();
    print('MyCombinedController initialized');
  }

  @override
  void onClose() {
    super.onClose();
    print('MyCombinedController closed');
  }
}

When to use each type of controller:

Use GetxController when you want to manage the state within a specific scope like a widget or a screen. It provides the onInit and onClose methods for initialization and cleanup.

Use GetxService when you want to manage global state or shared dependencies that can be accessed from anywhere in the app. It has a longer lifecycle compared to GetxController and can be used to maintain data across multiple screens or widgets.

Use GetxControllerMixin when you need to combine multiple controllers into a single class. This allows you to reuse and organize your code more effectively by separating different concerns into different controllers.

It’s important to note that all types of controllers in GetX should extend the Disposable Interface and use the dispose method to clean up any resources or subscriptions when they are no longer needed.

GetX Tutorial Part I

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.