Jonny Borges
Committed by GitHub

Merge pull request #720 from kranfix/get_life_cycle

Get life cycle
1 #The name of your workflow. 1 #The name of your workflow.
2 -name: build 2 +name: build
3 # Trigger the workflow on push or pull request 3 # Trigger the workflow on push or pull request
4 on: 4 on:
5 push: 5 push:
6 - branches: [ master ] 6 + branches: [master]
7 pull_request: 7 pull_request:
8 - branches: [ master ] 8 + branches: [master]
9 #A workflow run is made up of one or more jobs. Jobs run in parallel by default. 9 #A workflow run is made up of one or more jobs. Jobs run in parallel by default.
10 jobs: 10 jobs:
11 -  
12 test: 11 test:
13 #The type of machine to run the job on. [windows,macos, ubuntu , self-hosted] 12 #The type of machine to run the job on. [windows,macos, ubuntu , self-hosted]
14 defaults: 13 defaults:
@@ -24,8 +23,8 @@ jobs: @@ -24,8 +23,8 @@ jobs:
24 # https://github.com/marketplace/actions/flutter-action 23 # https://github.com/marketplace/actions/flutter-action
25 - uses: subosito/flutter-action@v1 24 - uses: subosito/flutter-action@v1
26 with: 25 with:
27 - flutter-version: '1.20.4'  
28 - channel: 'stable' 26 + flutter-version: "1.22.2"
  27 + channel: "stable"
29 - run: flutter pub get 28 - run: flutter pub get
30 #- run: flutter analyze 29 #- run: flutter analyze
31 # run flutter widgets tests and unit tests 30 # run flutter widgets tests and unit tests
@@ -33,4 +32,3 @@ jobs: @@ -33,4 +32,3 @@ jobs:
33 # Upload coverage reports to Codecov 32 # Upload coverage reports to Codecov
34 # https://github.com/marketplace/actions/codecov 33 # https://github.com/marketplace/actions/codecov
35 - uses: codecov/codecov-action@v1.0.7 34 - uses: codecov/codecov-action@v1.0.7
36 -  
@@ -19,7 +19,10 @@ class HomeController extends GetxController { @@ -19,7 +19,10 @@ class HomeController extends GetxController {
19 19
20 /// When the controller is initialized, make the http request 20 /// When the controller is initialized, make the http request
21 @override 21 @override
22 - void onInit() => fetchDataFromApi(); 22 + void onInit() {
  23 + super.onInit();
  24 + fetchDataFromApi();
  25 + }
23 26
24 /// fetch cases from Api 27 /// fetch cases from Api
25 Future<void> fetchDataFromApi() async { 28 Future<void> fetchDataFromApi() async {
@@ -170,7 +170,9 @@ class Controller extends GetxController { @@ -170,7 +170,9 @@ class Controller extends GetxController {
170 super.onReady(); 170 super.onReady();
171 } 171 }
172 172
  173 + @override
173 void onClose() { 174 void onClose() {
  175 + super.onClose();
174 print('onClose'); 176 print('onClose');
175 } 177 }
176 } 178 }
@@ -5,3 +5,5 @@ export 'src/get_main.dart'; @@ -5,3 +5,5 @@ export 'src/get_main.dart';
5 export 'src/log.dart'; 5 export 'src/log.dart';
6 6
7 export 'src/smart_management.dart'; 7 export 'src/smart_management.dart';
  8 +
  9 +export 'src/typedefs.dart';
  1 +typedef ValueUpdater<T> = T Function();
  1 +import 'package:meta/meta.dart';
  2 +import '../../get_core/get_core.dart';
  3 +
1 /// Special callable class to keep the contract of a regular method, and avoid 4 /// Special callable class to keep the contract of a regular method, and avoid
2 /// overrides if you extend the class that uses it, as Dart has no final 5 /// overrides if you extend the class that uses it, as Dart has no final
3 /// methods. 6 /// methods.
4 /// Used in [DisposableInterface] to avoid the danger of overriding onStart. 7 /// Used in [DisposableInterface] to avoid the danger of overriding onStart.
5 -///  
6 class _InternalFinalCallback<T> { 8 class _InternalFinalCallback<T> {
7 - T Function() callback; 9 + ValueUpdater<T> _callback;
8 10
9 - _InternalFinalCallback(); 11 + _InternalFinalCallback({ValueUpdater<T> callback}) : _callback = callback;
10 12
11 - T call() => callback.call(); 13 + T call() => _callback.call();
12 } 14 }
13 15
14 -mixin GetLifeCycle { 16 +/// The [GetLifeCycle]
  17 +///
  18 +/// ```dart
  19 +/// class SomeController with GetLifeCycle {
  20 +/// SomeController() {
  21 +/// initLifeCycle();
  22 +/// }
  23 +/// }
  24 +/// ```
  25 +mixin GetLifeCycle {
  26 + /// The `initLifeCycle` works as a constructor for the [GetLifeCycle]
  27 + ///
  28 + /// This method must be invoked in the constructor of the implementation
  29 + void initLifeCycle() {
  30 + onStart._callback = _onStart;
  31 + onDelete._callback = _onDelete;
  32 + }
  33 +
15 /// Called at the exact moment the widget is allocated in memory. 34 /// Called at the exact moment the widget is allocated in memory.
16 /// It uses an internal "callable" type, to avoid any @overrides in subclases. 35 /// It uses an internal "callable" type, to avoid any @overrides in subclases.
17 /// This method should be internal and is required to define the 36 /// This method should be internal and is required to define the
18 /// lifetime cycle of the subclass. 37 /// lifetime cycle of the subclass.
19 final onStart = _InternalFinalCallback<void>(); 38 final onStart = _InternalFinalCallback<void>();
20 39
  40 + /// Internal callback that starts the cycle of this controller.
21 final onDelete = _InternalFinalCallback<void>(); 41 final onDelete = _InternalFinalCallback<void>();
22 42
23 /// Called immediately after the widget is allocated in memory. 43 /// Called immediately after the widget is allocated in memory.
24 /// You might use this to initialize something for the controller. 44 /// You might use this to initialize something for the controller.
  45 + @mustCallSuper
25 void onInit() {} 46 void onInit() {}
26 47
27 /// Called 1 frame after onInit(). It is the perfect place to enter 48 /// Called 1 frame after onInit(). It is the perfect place to enter
28 /// navigation events, like snackbar, dialogs, or a new route, or 49 /// navigation events, like snackbar, dialogs, or a new route, or
29 /// async request. 50 /// async request.
  51 + @mustCallSuper
30 void onReady() {} 52 void onReady() {}
31 53
32 /// Called before [onDelete] method. [onClose] might be used to 54 /// Called before [onDelete] method. [onClose] might be used to
@@ -35,7 +57,32 @@ mixin GetLifeCycle { @@ -35,7 +57,32 @@ mixin GetLifeCycle {
35 /// Or dispose objects that can potentially create some memory leaks, 57 /// Or dispose objects that can potentially create some memory leaks,
36 /// like TextEditingControllers, AnimationControllers. 58 /// like TextEditingControllers, AnimationControllers.
37 /// Might be useful as well to persist some data on disk. 59 /// Might be useful as well to persist some data on disk.
  60 + @mustCallSuper
38 void onClose() {} 61 void onClose() {}
  62 +
  63 + bool _initialized = false;
  64 +
  65 + /// Checks whether the controller has already been initialized.
  66 + bool get initialized => _initialized;
  67 +
  68 + // Internal callback that starts the cycle of this controller.
  69 + void _onStart() {
  70 + if (_initialized) return;
  71 + onInit();
  72 + _initialized = true;
  73 + }
  74 +
  75 + bool _isClosed = false;
  76 +
  77 + /// Checks whether the controller has already been closed.
  78 + bool get isClosed => _isClosed;
  79 +
  80 + // Internal callback that starts the cycle of this controller.
  81 + void _onDelete() {
  82 + if (_isClosed) return;
  83 + _isClosed = true;
  84 + onClose();
  85 + }
39 } 86 }
40 87
41 /// Allow track difference between GetxServices and GetxControllers 88 /// Allow track difference between GetxServices and GetxControllers
  1 +import 'package:flutter/material.dart';
1 import 'package:flutter/scheduler.dart'; 2 import 'package:flutter/scheduler.dart';
2 import '../../../get_instance/src/lifecycle.dart'; 3 import '../../../get_instance/src/lifecycle.dart';
3 4
@@ -10,45 +11,24 @@ import '../../../get_instance/src/lifecycle.dart'; @@ -10,45 +11,24 @@ import '../../../get_instance/src/lifecycle.dart';
10 abstract class GetxService extends DisposableInterface with GetxServiceMixin {} 11 abstract class GetxService extends DisposableInterface with GetxServiceMixin {}
11 12
12 abstract class DisposableInterface with GetLifeCycle { 13 abstract class DisposableInterface with GetLifeCycle {
13 - bool _initialized = false;  
14 -  
15 - /// Checks whether the controller has already been initialized.  
16 - bool get initialized => _initialized;  
17 -  
18 - bool _isClosed = false;  
19 -  
20 - /// Checks whether the controller has already been closed.  
21 - bool get isClosed => _isClosed;  
22 -  
23 DisposableInterface() { 14 DisposableInterface() {
24 - onStart.callback = _onStart;  
25 - onDelete.callback = _onDelete;  
26 - }  
27 -  
28 - // Internal callback that starts the cycle of this controller.  
29 - void _onStart() {  
30 - if (_initialized) return;  
31 - onInit();  
32 - _initialized = true;  
33 - SchedulerBinding.instance?.addPostFrameCallback((_) => onReady());  
34 - }  
35 -  
36 - // Internal callback that starts the cycle of this controller.  
37 - void _onDelete() {  
38 - if (_isClosed) return;  
39 - _isClosed = true;  
40 - onClose(); 15 + initLifeCycle();
41 } 16 }
42 17
43 /// Called immediately after the widget is allocated in memory. 18 /// Called immediately after the widget is allocated in memory.
44 /// You might use this to initialize something for the controller. 19 /// You might use this to initialize something for the controller.
45 @override 20 @override
46 - void onInit() {} 21 + @mustCallSuper
  22 + void onInit() {
  23 + super.onInit();
  24 + SchedulerBinding.instance?.addPostFrameCallback((_) => onReady());
  25 + }
47 26
48 /// Called 1 frame after onInit(). It is the perfect place to enter 27 /// Called 1 frame after onInit(). It is the perfect place to enter
49 /// navigation events, like snackbar, dialogs, or a new route, or 28 /// navigation events, like snackbar, dialogs, or a new route, or
50 /// async request. 29 /// async request.
51 @override 30 @override
  31 + @mustCallSuper
52 void onReady() {} 32 void onReady() {}
53 33
54 /// Called before [onDelete] method. [onClose] might be used to 34 /// Called before [onDelete] method. [onClose] might be used to
@@ -38,36 +38,17 @@ typedef Condition = bool Function(); @@ -38,36 +38,17 @@ typedef Condition = bool Function();
38 38
39 abstract class GetNotifier<T> extends Value<T> with GetLifeCycle { 39 abstract class GetNotifier<T> extends Value<T> with GetLifeCycle {
40 GetNotifier(T initial) : super(initial) { 40 GetNotifier(T initial) : super(initial) {
41 - onStart.callback = _onStart;  
42 - onDelete.callback = _onDelete; 41 + initLifeCycle();
43 _fillEmptyStatus(); 42 _fillEmptyStatus();
44 } 43 }
45 44
46 - bool _initialized = false;  
47 -  
48 - /// Checks whether the controller has already been initialized.  
49 - bool get initialized => _initialized;  
50 -  
51 - bool _isClosed = false;  
52 -  
53 - /// Checks whether the controller has already been closed.  
54 - bool get isClosed => _isClosed;  
55 -  
56 - // Internal callback that starts the cycle of this controller.  
57 - void _onStart() {  
58 - if (_initialized) return;  
59 - onInit();  
60 - _initialized = true; 45 + @override
  46 + @mustCallSuper
  47 + void onInit() {
  48 + super.onInit();
61 SchedulerBinding.instance?.addPostFrameCallback((_) => onReady()); 49 SchedulerBinding.instance?.addPostFrameCallback((_) => onReady());
62 } 50 }
63 51
64 - // Internal callback that starts the cycle of this controller.  
65 - void _onDelete() {  
66 - if (_isClosed) return;  
67 - _isClosed = true;  
68 - onClose();  
69 - }  
70 -  
71 RxStatus _status; 52 RxStatus _status;
72 53
73 bool get isNullOrEmpty { 54 bool get isNullOrEmpty {
@@ -4,11 +4,12 @@ version: 3.13.2 @@ -4,11 +4,12 @@ version: 3.13.2
4 homepage: https://github.com/jonataslaw/getx 4 homepage: https://github.com/jonataslaw/getx
5 5
6 environment: 6 environment:
7 - sdk: ">=2.8.0 <3.0.0" 7 + sdk: ">=2.10.0 <3.0.0"
8 8
9 dependencies: 9 dependencies:
10 flutter: 10 flutter:
11 sdk: flutter 11 sdk: flutter
  12 + meta: 1.3.0-nullsafety.3
12 13
13 dev_dependencies: 14 dev_dependencies:
14 flutter_test: 15 flutter_test:
@@ -11,29 +11,7 @@ class Mock { @@ -11,29 +11,7 @@ class Mock {
11 11
12 class DisposableController with GetLifeCycle { 12 class DisposableController with GetLifeCycle {
13 DisposableController() { 13 DisposableController() {
14 - onStart.callback = _onStart;  
15 - onDelete.callback = _onDelete;  
16 - }  
17 -  
18 - // Internal callback that starts the cycle of this controller.  
19 - void _onStart() {  
20 - if (initialized) return;  
21 - onInit();  
22 - }  
23 -  
24 - // Internal callback that starts the cycle of this controller.  
25 - void _onDelete() {  
26 - if (isClosed) return;  
27 - isClosed = true;  
28 - onClose();  
29 - }  
30 -  
31 - bool initialized = false;  
32 -  
33 - bool isClosed = false;  
34 -  
35 - void onInit() async {  
36 - initialized = true; 14 + initLifeCycle();
37 } 15 }
38 } 16 }
39 17
@@ -73,7 +73,6 @@ void main() { @@ -73,7 +73,6 @@ void main() {
73 var expected = ''; 73 var expected = '';
74 void logFunction(String prefix, dynamic value, String info, 74 void logFunction(String prefix, dynamic value, String info,
75 {bool isError = false}) { 75 {bool isError = false}) {
76 - print('algo');  
77 expected = '$prefix $value $info'.trim(); 76 expected = '$prefix $value $info'.trim();
78 } 77 }
79 78