GetX Controller Life Cycle

 import "package:flutter/material.dart";

import "package:get/get.dart";
import "package:getx_flutter/Simple_State_Manager/simple_controller.dart";

void main() {
runApp(const MyApp(),);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: "Reactive State Manager",
theme: ThemeData(
primarySwatch: Colors.brown,
),
home: const AgainReact(),
);
}
}
class AgainReact extends StatelessWidget {
const AgainReact({super.key});

//SimpleReact simpleController = Get.put(SimpleReact(),);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Reactive State Manager"),
centerTitle: true,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GetBuilder<SimpleReact>(
init: SimpleReact(), ///Same as the "Get.put(SimpleReact(),);"
initState: (_) { return print("Controller instance is initialized"); },
dispose: (_) { return print("When Controller is removed from memory after that dispose method will be called"); },
builder: (simpleController) {
return Text(
"${simpleController.count}",
style: const TextStyle(fontSize: 30.0,),
);
},),
ElevatedButton(
onPressed: () {
///If we don't create controller instance initially then we have to find Controller and
///Get.find() will give same instance(of Controller) everytime and we can use throughout the app
var getController = Get.find<SimpleReact>();
getController.increment();

// simpleController.increment();
},
child: const Text("Increment"),
),
],
),
),
);
}
}
///Note:
///When we want to perform any action when Controller is initialized at that time :iniState: (_) {}"
///is used
///Always, All the business Logics(Like variable, methods...) will written in Controller class
/// and it is separate from UI

Comments

Popular posts from this blog

Pagination with Bloc Pattern in Flutter

Pagination First Practical in Flutter

ExpansionPanel with ExpansionPanelList with Complete Collapse Operation in Flutter