Created
April 8, 2021 15:17
-
-
Save RipplesCode/5d2b398d7a4b3d8c2210b79488d65604 to your computer and use it in GitHub Desktop.
Controller Life cycle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_getx/my_controller.dart'; | |
import 'package:flutter_getx/next_screen.dart'; | |
import 'package:flutter_getx/student.dart'; | |
import 'package:flutter_getx/unknown_route.dart'; | |
import 'package:get/get.dart'; | |
import 'home.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
MyController myController = Get.put(MyController()); | |
@override | |
Widget build(BuildContext context) { | |
// TODO: implement build | |
return GetMaterialApp( | |
title: "State Management", | |
home: Scaffold( | |
appBar: AppBar(title: Text("State Management")), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
GetBuilder<MyController>( | |
// initState: (data)=>myController.increment(), | |
//dispose: (_)=>myController.cleanUpTask(), | |
builder: (controller) { | |
return Text( | |
"The value is ${controller.count}", | |
style: TextStyle(fontSize: 25), | |
); | |
}, | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter_getx/student.dart'; | |
import 'package:get/get.dart'; | |
import 'home.dart'; | |
class MyController extends GetxController { | |
var count = 0; | |
void increment() async { | |
await Future<int>.delayed( | |
Duration(seconds: 5)); | |
this.count++; | |
update(); | |
} | |
void cleanUpTask() | |
{ | |
print("Clean up task"); | |
} | |
//Better Approach | |
@override | |
void onInit() { | |
print("Init Called"); | |
super.onInit(); | |
} | |
@override | |
void onReady() async { | |
super.onReady(); | |
} | |
@override | |
void onClose() { | |
super.onClose(); | |
} | |
} |
onInit
Called immediately after the widget is allocated in memory. You might use this to initialize something for the controller
onReady
Called after onInit(). It is the perfect place to enter navigation events, like snackbar, dialogs, or a new route, or async request.
If you are performing some async request then only it is required to use async else not.
I'm new to GetX.
I have recreated the this scenario and found my increment() method not working.
I have include the method under onInit() methods.
@OverRide
void onInit() {
increment(); // Added here
print("Init Called");
super.onInit();
}
My screen doesn't show number count increment.
Please advice on this?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In
my_controller.dart
onInit()
method will be called whenever the controller is initialized.onClose()
method will be called whenever the controller is removed from the memory.What about the
onReady()
method? What does it do or when would it be called and why is it async ?