Jermaine McFarlane

add replace method on GetInstance

... ... @@ -128,4 +128,12 @@ extension Inst on GetInterface {
/// [Get.lazyPut()], is registered in memory.
/// - [tag] optional, if you use a [tag] to register the Instance.
bool isPrepared<S>({String? tag}) => GetInstance().isPrepared<S>(tag: tag);
/// Replace a parent instance of a class in dependency management
/// with a [child] instance
void replace<P, C extends P>(C child, {String? tag, bool permanent = false}) {
delete<P>();
// ignore: unnecessary_cast
put(child as P, tag: tag, permanent: permanent);
}
}
... ...
import 'package:flutter_test/flutter_test.dart';
import 'package:get/get.dart';
import 'util/matcher.dart' as m;
class Mock {
... ... @@ -155,6 +156,13 @@ void main() {
expect(instance.initialized, true);
});
});
test('Get.replace test for replacing parent instance with child', () async {
Get.replace<DisposableController, Controller>(Controller());
final instance = Get.find<DisposableController>();
expect(instance is Controller, isTrue);
expect((instance as Controller).init, greaterThan(0));
});
}
class Controller extends DisposableController {
... ...