Getx In Flutter

Getx In Flutter :

GetX is an extra-light and powerful solution for Flutter.

Binding:

Bindings should be extended or implemented. When using [GetMaterialApp], all [GetPage]s and navigation methods (like Get.to()) have a [binding] property that takes an instance of Bindings to manage the dependencies() (via [Get.put()]) for the Route you are opening.

Navigation Screen GET method using in flutter:

  • 1)  Get.offNamed(): Current screen clear navigation history that is used to login or signup to home page.
  • 2)  Get.back(): Back the screen.
  • 3)  Get.toNamed(): New page navigation
  • 4)  Get.offAllNamed(): Used to all pages clear history for example logout.
Getx In Flutter

Data notify (Rebuilds data) Get in flutter

  1. obsuse then every declared .obsnot used String email. 

Add .obs to any variable and you make it observable. Basically you’re turning counter into a stream of type int. This means that we can listen to the changes made to counter from our view using GetX.

class Controller extends GetxController {
var counter = 0.obs;
voidincrement() =>counter.value++;
}

finalemail = ''.obs; //String email not declare 
RxDoubletotalAmount= 0.0.obs;
Rx<StateStatus>stateStatus = StateStatus.INITIAL.obs;

StringX	RxString
IntX	RxInt
MapX	RxMap
ListX	RxList
NumX	RxNum
DoubleX	RxDouble
Bool    RxBool

If you’ll be using with multiple controllers in the same widget. Obx doesn’t need a type, so it can be used with any number of controllers.

Obx(() => Text(email.value))

OR

GetX<LoginController>(
    builder: (controller) =>Text(controller.email.value));
  • 2. update() use then not used String email. 
finalemail = '';
voidcheckAutoValidate() {
autoValidate = true;
update(); //that is used to notify data
 }

GetBuilder<LoginController>(
    builder: (controller) => Text(controller.email)

Update() and .obs both are different method data rebuild get patter using flutter. update() method inside any method in the controller so that our widgets can listen to the changes made by the method. If you have used Provider package, it’s just like notifyListeners().

Get method call widget :

Get.dialog(Container(
alignment: FractionalOffset.center,
          child: CircularProgressIndicator(strokeWidth: 1)))

Get.arguments
Get.previousArguments
Get.previousRoute
Get.rawRoute
Get.routing
Get.isSnackbarOpen

Get.isDialogOpen
Get.isBottomSheetOpen

Get.removeRoute()
Get.until()Get.offUntil()
Get.offNamedUntil()

//Check in what platform the app is running
GetPlatform.isAndroid
GetPlatform.isIOS
GetPlatform.isMacOS
GetPlatform.isWindows
GetPlatform.isLinux
GetPlatform.isFuchsia

//Check the device type
GetPlatform.isMobile
GetPlatform.isDesktop
GetPlatform.isWeb

Get.height
Get.width

Get.context
Get.contextOverlay

context.width
context.height

context.heightTransformer()
context.widthTransformer()

context.mediaQuerySize()
context.mediaQueryPadding()

context.mediaQueryViewPadding()
context.mediaQueryViewInsets()

context.orientation()
context.isLandscape()

context.isPortrait()

context.devicePixelRatio()

/// Similar to MediaQuery.of(context).textScaleFactor;
context.textScaleFactor()
context.mediaQueryShortestSide()

context.showNavbar()
context.isPhone()

context.isSmallTablet()
context.isLargeTablet()
context.isTablet()
context.responsiveValue<T>()

DATA PASS :

GET ARGUMENT: Get.arguments

PASS ARGUMENT: Get.offNamed(ROUTE_HOME, arguments: ‘KAMLESH’);

Step: 1 Add the pubspec.yaml

get :

Step: 2 MaterialAppreplace  theGetMaterialApp

classMyAppextends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
        title: '',
        theme: ThemeData(
primarySwatch: Colors.green,
fontFamily: quickFont,
visualDensity: VisualDensity.adaptivePlatformDensity),
        home: SplashPage(),
initialBinding: SplashBinding(),
getPages: AppPages.pages);
  }
}

Step: 3 Pages

class AppPages {
static final List<GetPage>pages = [
GetPage(name: ROUTE_SPLASH, page: () =>SplashPage(), binding: SplashBinding()),
GetPage(name: ROUTE_LOGIN, page: () =>LoginPage(), binding: LoginBinding()),
GetPage(name: ROUTE_HOME, page: () =>HomePage(), binding: HomeBinding()),
GetPage(name: ROUTE_ORDER, page: () =>OrderPage(), binding: OrderBinding()),
GetPage(name: ROUTE_FAVOURITE, page: () =>FavouritePage(), binding: FavouriteBinding()),
  ];
}

Step: 4 Controller

Controller extends GetxController.

Put the business logic.

All the method and variable accessed from the view.

Access controller: Controller controller = Get.put(Controller());

GetX provides a class called GetxController which extends DisposableInterface.

This means that our controller will get deleted from memory as soon as the widgets using it are removed from the navigation stack. We don’t have to manually dispose anything and the memory consumption is reduced, resulting in high performance.

GetxController comes with onInit() and onClose() methods which essentially replace the initState() and dispose() methods of the StatefulWidget.

This allows us to completely avoid using StatefulWidget and write highly organized code.

First time api call :
@override
void onInit() {
callFavourite('');
super.onInit();
}

Step: 5 Binding

classSplashBindingextends Bindings {
@override
void dependencies() {
Get.lazyPut(() =>SplashController());
  }
}

Step: 6 Model class using value notify

class OrderResponse {
  List<Order>orderList;

OrderResponse({this.orderList});
}

class Order {
  Order({bool isFavourite}) {
this.setFavourite(isFavourite);
  }

RxBoolisFavourite= RxBool(null);
setFavourite(bool value) =>isFavourite.value= value;
}

That is put the controller file

UPDATE GET USING FLUTTER USING FAVOURITE

voidisFavorite(Order order) {
order.setFavourite(order.isFavourite.value? false : true);
}

INDEX USING UPDATE GET IN FLUTTER

voidbySetFavourite(categoryId) {
categoryList
.firstWhere((prod) =>prod.categoryId== categoryId)
          .isFavourite
.value =
      !categoryList
.firstWhere((prod) =>prod.categoryId== categoryId)
          .isFavourite
.value;
}

REMOVE GET USING FLUTTER :

voidremoveOrder(String orderId) {
intindexGet =
orderList.indexWhere((element) =>element.orderId.value== orderId);
orderList.removeAt(indexGet);
}

DOWNLOAD SOURCE CODE :

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application