Jonny Borges
Committed by GitHub

Merge pull request #720 from kranfix/get_life_cycle

Get life cycle
... ... @@ -3,12 +3,11 @@ name: build
# Trigger the workflow on push or pull request
on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]
#A workflow run is made up of one or more jobs. Jobs run in parallel by default.
jobs:
test:
#The type of machine to run the job on. [windows,macos, ubuntu , self-hosted]
defaults:
... ... @@ -24,8 +23,8 @@ jobs:
# https://github.com/marketplace/actions/flutter-action
- uses: subosito/flutter-action@v1
with:
flutter-version: '1.20.4'
channel: 'stable'
flutter-version: "1.22.2"
channel: "stable"
- run: flutter pub get
#- run: flutter analyze
# run flutter widgets tests and unit tests
... ... @@ -33,4 +32,3 @@ jobs:
# Upload coverage reports to Codecov
# https://github.com/marketplace/actions/codecov
- uses: codecov/codecov-action@v1.0.7
... ...
... ... @@ -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 {
... ...
... ... @@ -170,7 +170,9 @@ class Controller extends GetxController {
super.onReady();
}
@override
void onClose() {
super.onClose();
print('onClose');
}
}
... ...
... ... @@ -5,3 +5,5 @@ export 'src/get_main.dart';
export 'src/log.dart';
export 'src/smart_management.dart';
export 'src/typedefs.dart';
... ...
typedef ValueUpdater<T> = T Function();
... ...
import 'package:meta/meta.dart';
import '../../get_core/get_core.dart';
/// Special callable class to keep the contract of a regular method, and avoid
/// 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;
ValueUpdater<T> _callback;
_InternalFinalCallback();
_InternalFinalCallback({ValueUpdater<T> callback}) : _callback = callback;
T call() => callback.call();
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
/// lifetime cycle of the subclass.
final onStart = _InternalFinalCallback<void>();
/// Internal callback that starts the cycle of this controller.
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() {}
/// Called 1 frame after onInit(). It is the perfect place to enter
/// navigation events, like snackbar, dialogs, or a new route, or
/// async request.
@mustCallSuper
void onReady() {}
/// Called before [onDelete] method. [onClose] might be used to
... ... @@ -35,7 +57,32 @@ mixin GetLifeCycle {
/// Or dispose objects that can potentially create some memory leaks,
/// like TextEditingControllers, AnimationControllers.
/// Might be useful as well to persist some data on disk.
@mustCallSuper
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.
bool get isClosed => _isClosed;
// Internal callback that starts the cycle of this controller.
void _onDelete() {
if (_isClosed) return;
_isClosed = true;
onClose();
}
}
/// Allow track difference between GetxServices and GetxControllers
... ...
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import '../../../get_instance/src/lifecycle.dart';
... ... @@ -10,45 +11,24 @@ 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;
bool _isClosed = false;
/// Checks whether the controller has already been closed.
bool get isClosed => _isClosed;
DisposableInterface() {
onStart.callback = _onStart;
onDelete.callback = _onDelete;
}
// Internal callback that starts the cycle of this controller.
void _onStart() {
if (_initialized) return;
onInit();
_initialized = true;
SchedulerBinding.instance?.addPostFrameCallback((_) => onReady());
}
// Internal callback that starts the cycle of this controller.
void _onDelete() {
if (_isClosed) return;
_isClosed = true;
onClose();
initLifeCycle();
}
/// Called immediately after the widget is allocated in memory.
/// You might use this to initialize something for the controller.
@override
void onInit() {}
@mustCallSuper
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
/// async request.
@override
@mustCallSuper
void onReady() {}
/// Called before [onDelete] method. [onClose] might be used to
... ...
... ... @@ -38,36 +38,17 @@ typedef Condition = bool Function();
abstract class GetNotifier<T> extends Value<T> with GetLifeCycle {
GetNotifier(T initial) : super(initial) {
onStart.callback = _onStart;
onDelete.callback = _onDelete;
initLifeCycle();
_fillEmptyStatus();
}
bool _initialized = false;
/// Checks whether the controller has already been initialized.
bool get initialized => _initialized;
bool _isClosed = false;
/// Checks whether the controller has already been closed.
bool get isClosed => _isClosed;
// 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());
}
// Internal callback that starts the cycle of this controller.
void _onDelete() {
if (_isClosed) return;
_isClosed = true;
onClose();
}
RxStatus _status;
bool get isNullOrEmpty {
... ...
... ... @@ -4,11 +4,12 @@ version: 3.13.2
homepage: https://github.com/jonataslaw/getx
environment:
sdk: ">=2.8.0 <3.0.0"
sdk: ">=2.10.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
meta: 1.3.0-nullsafety.3
dev_dependencies:
flutter_test:
... ...
... ... @@ -11,29 +11,7 @@ class Mock {
class DisposableController with GetLifeCycle {
DisposableController() {
onStart.callback = _onStart;
onDelete.callback = _onDelete;
}
// Internal callback that starts the cycle of this controller.
void _onStart() {
if (initialized) return;
onInit();
}
// Internal callback that starts the cycle of this controller.
void _onDelete() {
if (isClosed) return;
isClosed = true;
onClose();
}
bool initialized = false;
bool isClosed = false;
void onInit() async {
initialized = true;
initLifeCycle();
}
}
... ...
... ... @@ -73,7 +73,6 @@ void main() {
var expected = '';
void logFunction(String prefix, dynamic value, String info,
{bool isError = false}) {
print('algo');
expected = '$prefix $value $info'.trim();
}
... ...