life cycle control logic only dependends on GetLifeCycle and not in the implementations
Showing
4 changed files
with
47 additions
and
43 deletions
@@ -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 { |
@@ -4,24 +4,32 @@ import 'package:flutter/material.dart'; | @@ -4,24 +4,32 @@ import 'package:flutter/material.dart'; | ||
4 | /// 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 |
5 | /// methods. | 5 | /// methods. |
6 | /// Used in [DisposableInterface] to avoid the danger of overriding onStart. | 6 | /// Used in [DisposableInterface] to avoid the danger of overriding onStart. |
7 | -/// | ||
8 | class _InternalFinalCallback<T> { | 7 | class _InternalFinalCallback<T> { |
9 | - T Function() callback; | ||
10 | - | ||
11 | - _InternalFinalCallback({this.callback}); | ||
12 | - | ||
13 | - T call() => callback.call(); | ||
14 | -} | 8 | + ValueGetter<T> _callback; |
15 | 9 | ||
16 | -class _InternalFinalPrivateCallback<T> { | ||
17 | - T Function() _callback; | ||
18 | - | ||
19 | - _InternalFinalPrivateCallback(); | 10 | + _InternalFinalCallback({ValueGetter<T> callback}) : _callback = callback; |
20 | 11 | ||
21 | T call() => _callback.call(); | 12 | T call() => _callback.call(); |
22 | } | 13 | } |
23 | 14 | ||
15 | +/// The [GetLifeCycle] | ||
16 | +/// | ||
17 | +/// ```dart | ||
18 | +/// class SomeController with GetLifeCycle { | ||
19 | +/// SomeController() { | ||
20 | +/// initLifeCycle(); | ||
21 | +/// } | ||
22 | +/// } | ||
23 | +/// ``` | ||
24 | mixin GetLifeCycle { | 24 | mixin GetLifeCycle { |
25 | + /// The `initLifeCycle` works as a constructor for the [GetLifeCycle] | ||
26 | + /// | ||
27 | + /// This method must be invoked in the constructor of the implementation | ||
28 | + void initLifeCycle() { | ||
29 | + onStart._callback = _onStart; | ||
30 | + onDelete._callback = _onDelete; | ||
31 | + } | ||
32 | + | ||
25 | /// Called at the exact moment the widget is allocated in memory. | 33 | /// Called at the exact moment the widget is allocated in memory. |
26 | /// It uses an internal "callable" type, to avoid any @overrides in subclases. | 34 | /// It uses an internal "callable" type, to avoid any @overrides in subclases. |
27 | /// This method should be internal and is required to define the | 35 | /// This method should be internal and is required to define the |
@@ -29,14 +37,12 @@ mixin GetLifeCycle { | @@ -29,14 +37,12 @@ mixin GetLifeCycle { | ||
29 | final onStart = _InternalFinalCallback<void>(); | 37 | final onStart = _InternalFinalCallback<void>(); |
30 | 38 | ||
31 | /// Internal callback that starts the cycle of this controller. | 39 | /// Internal callback that starts the cycle of this controller. |
32 | - final onDelete = _InternalFinalPrivateCallback<void>(); | 40 | + final onDelete = _InternalFinalCallback<void>(); |
33 | 41 | ||
34 | /// Called immediately after the widget is allocated in memory. | 42 | /// Called immediately after the widget is allocated in memory. |
35 | /// You might use this to initialize something for the controller. | 43 | /// You might use this to initialize something for the controller. |
36 | @mustCallSuper | 44 | @mustCallSuper |
37 | - void onInit() { | ||
38 | - onDelete._callback = _onDelete; | ||
39 | - } | 45 | + void onInit() {} |
40 | 46 | ||
41 | /// Called 1 frame after onInit(). It is the perfect place to enter | 47 | /// Called 1 frame after onInit(). It is the perfect place to enter |
42 | /// navigation events, like snackbar, dialogs, or a new route, or | 48 | /// navigation events, like snackbar, dialogs, or a new route, or |
@@ -51,6 +57,18 @@ mixin GetLifeCycle { | @@ -51,6 +57,18 @@ mixin GetLifeCycle { | ||
51 | /// Might be useful as well to persist some data on disk. | 57 | /// Might be useful as well to persist some data on disk. |
52 | void onClose() {} | 58 | void onClose() {} |
53 | 59 | ||
60 | + bool _initialized = false; | ||
61 | + | ||
62 | + /// Checks whether the controller has already been initialized. | ||
63 | + bool get initialized => _initialized; | ||
64 | + | ||
65 | + // Internal callback that starts the cycle of this controller. | ||
66 | + void _onStart() { | ||
67 | + if (_initialized) return; | ||
68 | + onInit(); | ||
69 | + _initialized = true; | ||
70 | + } | ||
71 | + | ||
54 | bool _isClosed = false; | 72 | bool _isClosed = false; |
55 | 73 | ||
56 | /// Checks whether the controller has already been closed. | 74 | /// Checks whether the controller has already been closed. |
@@ -11,28 +11,18 @@ import '../../../get_instance/src/lifecycle.dart'; | @@ -11,28 +11,18 @@ import '../../../get_instance/src/lifecycle.dart'; | ||
11 | abstract class GetxService extends DisposableInterface with GetxServiceMixin {} | 11 | abstract class GetxService extends DisposableInterface with GetxServiceMixin {} |
12 | 12 | ||
13 | abstract class DisposableInterface with GetLifeCycle { | 13 | abstract class DisposableInterface with GetLifeCycle { |
14 | - bool _initialized = false; | ||
15 | - | ||
16 | - /// Checks whether the controller has already been initialized. | ||
17 | - bool get initialized => _initialized; | ||
18 | - | ||
19 | DisposableInterface() { | 14 | DisposableInterface() { |
20 | - onStart.callback = _onStart; | ||
21 | - } | ||
22 | - | ||
23 | - // Internal callback that starts the cycle of this controller. | ||
24 | - void _onStart() { | ||
25 | - if (_initialized) return; | ||
26 | - onInit(); | ||
27 | - _initialized = true; | ||
28 | - SchedulerBinding.instance?.addPostFrameCallback((_) => onReady()); | 15 | + initLifeCycle(); |
29 | } | 16 | } |
30 | 17 | ||
31 | /// Called immediately after the widget is allocated in memory. | 18 | /// Called immediately after the widget is allocated in memory. |
32 | /// You might use this to initialize something for the controller. | 19 | /// You might use this to initialize something for the controller. |
33 | @override | 20 | @override |
34 | @mustCallSuper | 21 | @mustCallSuper |
35 | - void onInit() => super.onInit(); | 22 | + void onInit() { |
23 | + super.onInit(); | ||
24 | + SchedulerBinding.instance?.addPostFrameCallback((_) => onReady()); | ||
25 | + } | ||
36 | 26 | ||
37 | /// 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 |
38 | /// navigation events, like snackbar, dialogs, or a new route, or | 28 | /// navigation events, like snackbar, dialogs, or a new route, or |
@@ -48,6 +38,5 @@ abstract class DisposableInterface with GetLifeCycle { | @@ -48,6 +38,5 @@ abstract class DisposableInterface with GetLifeCycle { | ||
48 | /// like TextEditingControllers, AnimationControllers. | 38 | /// like TextEditingControllers, AnimationControllers. |
49 | /// Might be useful as well to persist some data on disk. | 39 | /// Might be useful as well to persist some data on disk. |
50 | @override | 40 | @override |
51 | - @mustCallSuper | ||
52 | void onClose() {} | 41 | void onClose() {} |
53 | } | 42 | } |
@@ -38,20 +38,14 @@ typedef Condition = bool Function(); | @@ -38,20 +38,14 @@ 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; | 41 | + initLifeCycle(); |
42 | _fillEmptyStatus(); | 42 | _fillEmptyStatus(); |
43 | } | 43 | } |
44 | 44 | ||
45 | - bool _initialized = false; | ||
46 | - | ||
47 | - /// Checks whether the controller has already been initialized. | ||
48 | - bool get initialized => _initialized; | ||
49 | - | ||
50 | - // Internal callback that starts the cycle of this controller. | ||
51 | - void _onStart() { | ||
52 | - if (_initialized) return; | ||
53 | - onInit(); | ||
54 | - _initialized = true; | 45 | + @override |
46 | + @mustCallSuper | ||
47 | + void onInit() { | ||
48 | + super.onInit(); | ||
55 | SchedulerBinding.instance?.addPostFrameCallback((_) => onReady()); | 49 | SchedulerBinding.instance?.addPostFrameCallback((_) => onReady()); |
56 | } | 50 | } |
57 | 51 |
-
Please register or login to post a comment