Frank Moreno

life cycle control logic only dependends on GetLifeCycle and not in the implementations

... ... @@ -19,7 +19,10 @@ class HomeController extends GetxController {
/// When the controller is initialized, make the http request
@override
void onInit() => fetchDataFromApi();
void onInit() {
super.onInit();
fetchDataFromApi();
}
/// fetch cases from Api
Future<void> fetchDataFromApi() async {
... ...
... ... @@ -4,24 +4,32 @@ import 'package:flutter/material.dart';
/// overrides if you extend the class that uses it, as Dart has no final
/// methods.
/// Used in [DisposableInterface] to avoid the danger of overriding onStart.
///
class _InternalFinalCallback<T> {
T Function() callback;
_InternalFinalCallback({this.callback});
T call() => callback.call();
}
ValueGetter<T> _callback;
class _InternalFinalPrivateCallback<T> {
T Function() _callback;
_InternalFinalPrivateCallback();
_InternalFinalCallback({ValueGetter<T> callback}) : _callback = callback;
T call() => _callback.call();
}
/// The [GetLifeCycle]
///
/// ```dart
/// class SomeController with GetLifeCycle {
/// SomeController() {
/// initLifeCycle();
/// }
/// }
/// ```
mixin GetLifeCycle {
/// The `initLifeCycle` works as a constructor for the [GetLifeCycle]
///
/// This method must be invoked in the constructor of the implementation
void initLifeCycle() {
onStart._callback = _onStart;
onDelete._callback = _onDelete;
}
/// Called at the exact moment the widget is allocated in memory.
/// It uses an internal "callable" type, to avoid any @overrides in subclases.
/// This method should be internal and is required to define the
... ... @@ -29,14 +37,12 @@ mixin GetLifeCycle {
final onStart = _InternalFinalCallback<void>();
/// Internal callback that starts the cycle of this controller.
final onDelete = _InternalFinalPrivateCallback<void>();
final onDelete = _InternalFinalCallback<void>();
/// Called immediately after the widget is allocated in memory.
/// You might use this to initialize something for the controller.
@mustCallSuper
void onInit() {
onDelete._callback = _onDelete;
}
void onInit() {}
/// Called 1 frame after onInit(). It is the perfect place to enter
/// navigation events, like snackbar, dialogs, or a new route, or
... ... @@ -51,6 +57,18 @@ mixin GetLifeCycle {
/// Might be useful as well to persist some data on disk.
void onClose() {}
bool _initialized = false;
/// Checks whether the controller has already been initialized.
bool get initialized => _initialized;
// Internal callback that starts the cycle of this controller.
void _onStart() {
if (_initialized) return;
onInit();
_initialized = true;
}
bool _isClosed = false;
/// Checks whether the controller has already been closed.
... ...
... ... @@ -11,28 +11,18 @@ import '../../../get_instance/src/lifecycle.dart';
abstract class GetxService extends DisposableInterface with GetxServiceMixin {}
abstract class DisposableInterface with GetLifeCycle {
bool _initialized = false;
/// Checks whether the controller has already been initialized.
bool get initialized => _initialized;
DisposableInterface() {
onStart.callback = _onStart;
}
// Internal callback that starts the cycle of this controller.
void _onStart() {
if (_initialized) return;
onInit();
_initialized = true;
SchedulerBinding.instance?.addPostFrameCallback((_) => onReady());
initLifeCycle();
}
/// Called immediately after the widget is allocated in memory.
/// You might use this to initialize something for the controller.
@override
@mustCallSuper
void onInit() => super.onInit();
void onInit() {
super.onInit();
SchedulerBinding.instance?.addPostFrameCallback((_) => onReady());
}
/// Called 1 frame after onInit(). It is the perfect place to enter
/// navigation events, like snackbar, dialogs, or a new route, or
... ... @@ -48,6 +38,5 @@ abstract class DisposableInterface with GetLifeCycle {
/// like TextEditingControllers, AnimationControllers.
/// Might be useful as well to persist some data on disk.
@override
@mustCallSuper
void onClose() {}
}
... ...
... ... @@ -38,20 +38,14 @@ typedef Condition = bool Function();
abstract class GetNotifier<T> extends Value<T> with GetLifeCycle {
GetNotifier(T initial) : super(initial) {
onStart.callback = _onStart;
initLifeCycle();
_fillEmptyStatus();
}
bool _initialized = false;
/// Checks whether the controller has already been initialized.
bool get initialized => _initialized;
// Internal callback that starts the cycle of this controller.
void _onStart() {
if (_initialized) return;
onInit();
_initialized = true;
@override
@mustCallSuper
void onInit() {
super.onInit();
SchedulerBinding.instance?.addPostFrameCallback((_) => onReady());
}
... ...