Skip to content

Instantly share code, notes, and snippets.

@RipplesCode
Created April 8, 2021 15:20
Show Gist options
  • Save RipplesCode/0a255c016f6925c2c30e6f922c248a71 to your computer and use it in GitHub Desktop.
Save RipplesCode/0a255c016f6925c2c30e6f922c248a71 to your computer and use it in GitHub Desktop.
Get.lazyput Get.putAsync Get.create
import 'package:flutter/material.dart';
import 'package:flutter_getx/my_controller.dart';
import 'package:get/get.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// My Controller instance will be created even if it is not used
// tag will be used to find the instance with tag name
// Controller disposed when it is not used but if permanent is
//true the instance will be alive throughout the app
//MyController myController = Get.put(MyController(),tag: "instance1",permanent: true);
// Instance will be created when it is used
//It is similar to "permanent", the difference is that the instance
// is discarded when is not being used,
// but when it's use is needed again, Get will recreate the instance
//Get.lazyPut(() => MyController(),tag:"instance",fenix: false);
// Get.putAsync<MyController>(() async => await MyController());
// Here permanent will be true by default and isSingleton is false
// Get.Create<SomeClass>(() => SomeClass());
// Get.Create<LoginController>(() => LoginController());
// TODO: implement build
return GetMaterialApp(
title: "Dependency Injection",
home: Scaffold(
appBar: AppBar(title: Text("Dependency Injection")),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
RaisedButton(
child: Text("Click Me"),
onPressed: (){
// Instance will be created
// Get.find<MyController>();
//Find instance with tag name
//Get.find<MyController>().incrementCounter();
},
),
],
),
),
),
);
}
}
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
class MyController extends GetxController {
void incrementCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
int counter = (prefs.getInt('counter') ?? 0) + 1;
print('Pressed $counter times.');
await prefs.setInt('counter', counter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment