Showing
4 changed files
with
36 additions
and
38 deletions
1 | +import 'package:flutter/material.dart'; | ||
2 | + | ||
1 | /// Special callable class to keep the contract of a regular method, and avoid | 3 | /// 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 | 4 | /// overrides if you extend the class that uses it, as Dart has no final |
3 | /// methods. | 5 | /// methods. |
@@ -6,11 +8,19 @@ | @@ -6,11 +8,19 @@ | ||
6 | class _InternalFinalCallback<T> { | 8 | class _InternalFinalCallback<T> { |
7 | T Function() callback; | 9 | T Function() callback; |
8 | 10 | ||
9 | - _InternalFinalCallback(); | 11 | + _InternalFinalCallback({this.callback}); |
10 | 12 | ||
11 | T call() => callback.call(); | 13 | T call() => callback.call(); |
12 | } | 14 | } |
13 | 15 | ||
16 | +class _InternalFinalPrivateCallback<T> { | ||
17 | + T Function() _callback; | ||
18 | + | ||
19 | + _InternalFinalPrivateCallback(); | ||
20 | + | ||
21 | + T call() => _callback.call(); | ||
22 | +} | ||
23 | + | ||
14 | mixin GetLifeCycle { | 24 | mixin GetLifeCycle { |
15 | /// Called at the exact moment the widget is allocated in memory. | 25 | /// Called at the exact moment the widget is allocated in memory. |
16 | /// It uses an internal "callable" type, to avoid any @overrides in subclases. | 26 | /// It uses an internal "callable" type, to avoid any @overrides in subclases. |
@@ -18,11 +28,15 @@ mixin GetLifeCycle { | @@ -18,11 +28,15 @@ mixin GetLifeCycle { | ||
18 | /// lifetime cycle of the subclass. | 28 | /// lifetime cycle of the subclass. |
19 | final onStart = _InternalFinalCallback<void>(); | 29 | final onStart = _InternalFinalCallback<void>(); |
20 | 30 | ||
21 | - final onDelete = _InternalFinalCallback<void>(); | 31 | + /// Internal callback that starts the cycle of this controller. |
32 | + final onDelete = _InternalFinalPrivateCallback<void>(); | ||
22 | 33 | ||
23 | /// Called immediately after the widget is allocated in memory. | 34 | /// Called immediately after the widget is allocated in memory. |
24 | /// You might use this to initialize something for the controller. | 35 | /// You might use this to initialize something for the controller. |
25 | - void onInit() {} | 36 | + @mustCallSuper |
37 | + void onInit() { | ||
38 | + onDelete._callback = _onDelete; | ||
39 | + } | ||
26 | 40 | ||
27 | /// Called 1 frame after onInit(). It is the perfect place to enter | 41 | /// Called 1 frame after onInit(). It is the perfect place to enter |
28 | /// navigation events, like snackbar, dialogs, or a new route, or | 42 | /// navigation events, like snackbar, dialogs, or a new route, or |
@@ -36,6 +50,18 @@ mixin GetLifeCycle { | @@ -36,6 +50,18 @@ mixin GetLifeCycle { | ||
36 | /// like TextEditingControllers, AnimationControllers. | 50 | /// like TextEditingControllers, AnimationControllers. |
37 | /// Might be useful as well to persist some data on disk. | 51 | /// Might be useful as well to persist some data on disk. |
38 | void onClose() {} | 52 | void onClose() {} |
53 | + | ||
54 | + bool _isClosed = false; | ||
55 | + | ||
56 | + /// Checks whether the controller has already been closed. | ||
57 | + bool get isClosed => _isClosed; | ||
58 | + | ||
59 | + // Internal callback that starts the cycle of this controller. | ||
60 | + void _onDelete() { | ||
61 | + if (_isClosed) return; | ||
62 | + _isClosed = true; | ||
63 | + onClose(); | ||
64 | + } | ||
39 | } | 65 | } |
40 | 66 | ||
41 | /// Allow track difference between GetxServices and GetxControllers | 67 | /// 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 | ||
@@ -15,14 +16,8 @@ abstract class DisposableInterface with GetLifeCycle { | @@ -15,14 +16,8 @@ abstract class DisposableInterface with GetLifeCycle { | ||
15 | /// Checks whether the controller has already been initialized. | 16 | /// Checks whether the controller has already been initialized. |
16 | bool get initialized => _initialized; | 17 | bool get initialized => _initialized; |
17 | 18 | ||
18 | - bool _isClosed = false; | ||
19 | - | ||
20 | - /// Checks whether the controller has already been closed. | ||
21 | - bool get isClosed => _isClosed; | ||
22 | - | ||
23 | DisposableInterface() { | 19 | DisposableInterface() { |
24 | onStart.callback = _onStart; | 20 | onStart.callback = _onStart; |
25 | - onDelete.callback = _onDelete; | ||
26 | } | 21 | } |
27 | 22 | ||
28 | // Internal callback that starts the cycle of this controller. | 23 | // Internal callback that starts the cycle of this controller. |
@@ -33,22 +28,17 @@ abstract class DisposableInterface with GetLifeCycle { | @@ -33,22 +28,17 @@ abstract class DisposableInterface with GetLifeCycle { | ||
33 | SchedulerBinding.instance?.addPostFrameCallback((_) => onReady()); | 28 | SchedulerBinding.instance?.addPostFrameCallback((_) => onReady()); |
34 | } | 29 | } |
35 | 30 | ||
36 | - // Internal callback that starts the cycle of this controller. | ||
37 | - void _onDelete() { | ||
38 | - if (_isClosed) return; | ||
39 | - _isClosed = true; | ||
40 | - onClose(); | ||
41 | - } | ||
42 | - | ||
43 | /// Called immediately after the widget is allocated in memory. | 31 | /// Called immediately after the widget is allocated in memory. |
44 | /// You might use this to initialize something for the controller. | 32 | /// You might use this to initialize something for the controller. |
45 | @override | 33 | @override |
46 | - void onInit() {} | 34 | + @mustCallSuper |
35 | + void onInit() => super.onInit(); | ||
47 | 36 | ||
48 | /// Called 1 frame after onInit(). It is the perfect place to enter | 37 | /// Called 1 frame after onInit(). It is the perfect place to enter |
49 | /// navigation events, like snackbar, dialogs, or a new route, or | 38 | /// navigation events, like snackbar, dialogs, or a new route, or |
50 | /// async request. | 39 | /// async request. |
51 | @override | 40 | @override |
41 | + @mustCallSuper | ||
52 | void onReady() {} | 42 | void onReady() {} |
53 | 43 | ||
54 | /// Called before [onDelete] method. [onClose] might be used to | 44 | /// Called before [onDelete] method. [onClose] might be used to |
@@ -58,5 +48,6 @@ abstract class DisposableInterface with GetLifeCycle { | @@ -58,5 +48,6 @@ abstract class DisposableInterface with GetLifeCycle { | ||
58 | /// like TextEditingControllers, AnimationControllers. | 48 | /// like TextEditingControllers, AnimationControllers. |
59 | /// Might be useful as well to persist some data on disk. | 49 | /// Might be useful as well to persist some data on disk. |
60 | @override | 50 | @override |
51 | + @mustCallSuper | ||
61 | void onClose() {} | 52 | void onClose() {} |
62 | } | 53 | } |
@@ -39,7 +39,6 @@ typedef Condition = bool Function(); | @@ -39,7 +39,6 @@ typedef Condition = bool Function(); | ||
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; | 41 | onStart.callback = _onStart; |
42 | - onDelete.callback = _onDelete; | ||
43 | _fillEmptyStatus(); | 42 | _fillEmptyStatus(); |
44 | } | 43 | } |
45 | 44 | ||
@@ -48,11 +47,6 @@ abstract class GetNotifier<T> extends Value<T> with GetLifeCycle { | @@ -48,11 +47,6 @@ abstract class GetNotifier<T> extends Value<T> with GetLifeCycle { | ||
48 | /// Checks whether the controller has already been initialized. | 47 | /// Checks whether the controller has already been initialized. |
49 | bool get initialized => _initialized; | 48 | bool get initialized => _initialized; |
50 | 49 | ||
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. | 50 | // Internal callback that starts the cycle of this controller. |
57 | void _onStart() { | 51 | void _onStart() { |
58 | if (_initialized) return; | 52 | if (_initialized) return; |
@@ -61,13 +55,6 @@ abstract class GetNotifier<T> extends Value<T> with GetLifeCycle { | @@ -61,13 +55,6 @@ abstract class GetNotifier<T> extends Value<T> with GetLifeCycle { | ||
61 | SchedulerBinding.instance?.addPostFrameCallback((_) => onReady()); | 55 | SchedulerBinding.instance?.addPostFrameCallback((_) => onReady()); |
62 | } | 56 | } |
63 | 57 | ||
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; | 58 | RxStatus _status; |
72 | 59 | ||
73 | bool get isNullOrEmpty { | 60 | bool get isNullOrEmpty { |
@@ -12,7 +12,6 @@ class Mock { | @@ -12,7 +12,6 @@ class Mock { | ||
12 | class DisposableController with GetLifeCycle { | 12 | class DisposableController with GetLifeCycle { |
13 | DisposableController() { | 13 | DisposableController() { |
14 | onStart.callback = _onStart; | 14 | onStart.callback = _onStart; |
15 | - onDelete.callback = _onDelete; | ||
16 | } | 15 | } |
17 | 16 | ||
18 | // Internal callback that starts the cycle of this controller. | 17 | // Internal callback that starts the cycle of this controller. |
@@ -21,18 +20,13 @@ class DisposableController with GetLifeCycle { | @@ -21,18 +20,13 @@ class DisposableController with GetLifeCycle { | ||
21 | onInit(); | 20 | onInit(); |
22 | } | 21 | } |
23 | 22 | ||
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; | 23 | bool initialized = false; |
32 | 24 | ||
33 | bool isClosed = false; | 25 | bool isClosed = false; |
34 | 26 | ||
27 | + @override | ||
35 | void onInit() async { | 28 | void onInit() async { |
29 | + super.onInit(); | ||
36 | initialized = true; | 30 | initialized = true; |
37 | } | 31 | } |
38 | } | 32 | } |
-
Please register or login to post a comment