Skip to content

Instantly share code, notes, and snippets.

@RipplesCode
Created April 8, 2021 15:17
Show Gist options
  • Save RipplesCode/5d2b398d7a4b3d8c2210b79488d65604 to your computer and use it in GitHub Desktop.
Save RipplesCode/5d2b398d7a4b3d8c2210b79488d65604 to your computer and use it in GitHub Desktop.
Controller Life cycle
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),
);
},
),
],
),
),
),
);
}
}
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();
}
}
@Nantha84
Copy link

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