GetX Package in Flutter
GetX is an extra-light and powerful solution for Flutter. It combines high-performance state management, intelligent dependency injection, and route management quickly and practically
Installing
dependencies:
get: ^4.1.4
However, most times to achieve things like navigating to screens, state management, and show alerts, a lot of boilerplates are needed
Let’s say you want to navigate to a screen called SecondScreen. you will have to write:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
but in GetX Package :
Get.to(SecondScreen());
When you need to navigate back to the previous page in Flutter you will have to write
Navigator.pop(context);
but in GetX like this :
Get.back();
GetxController
import 'package:get/get.dart';
class MyHomePageController extends GetxController {
final count = 0.obs;
increment() => count.value++;
}
class MyHomePage extends StatelessWidget {
final String title;
final MyHomePageController controller = Get.put(MyHomePageController());
MyHomePage({this.title});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Obx(
() => Text(
'${controller.count.value}',
style: Theme.of(context).textTheme.headline4,
),
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: controller.increment,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Snackbar
Get.snackbar('GetX Snackbar', 'Yay! Awesome GetX Snackbar');
Conclusion
GetX was created to improve the productivity of Flutter developers as they build out features. Instead of having to search for boilerplate needed to do things like state management, navigation management, and more, GetX provides a simple intuitive API to achieve these activities without sacrificing performance
If you find more detail click here : https://pub.dev/packages/get