Jermaine McFarlane

add replace method on GetInstance

@@ -128,4 +128,12 @@ extension Inst on GetInterface { @@ -128,4 +128,12 @@ extension Inst on GetInterface {
128 /// [Get.lazyPut()], is registered in memory. 128 /// [Get.lazyPut()], is registered in memory.
129 /// - [tag] optional, if you use a [tag] to register the Instance. 129 /// - [tag] optional, if you use a [tag] to register the Instance.
130 bool isPrepared<S>({String? tag}) => GetInstance().isPrepared<S>(tag: tag); 130 bool isPrepared<S>({String? tag}) => GetInstance().isPrepared<S>(tag: tag);
  131 +
  132 + /// Replace a parent instance of a class in dependency management
  133 + /// with a [child] instance
  134 + void replace<P, C extends P>(C child, {String? tag, bool permanent = false}) {
  135 + delete<P>();
  136 + // ignore: unnecessary_cast
  137 + put(child as P, tag: tag, permanent: permanent);
  138 + }
131 } 139 }
1 import 'package:flutter_test/flutter_test.dart'; 1 import 'package:flutter_test/flutter_test.dart';
2 import 'package:get/get.dart'; 2 import 'package:get/get.dart';
  3 +
3 import 'util/matcher.dart' as m; 4 import 'util/matcher.dart' as m;
4 5
5 class Mock { 6 class Mock {
@@ -155,6 +156,13 @@ void main() { @@ -155,6 +156,13 @@ void main() {
155 expect(instance.initialized, true); 156 expect(instance.initialized, true);
156 }); 157 });
157 }); 158 });
  159 +
  160 + test('Get.replace test for replacing parent instance with child', () async {
  161 + Get.replace<DisposableController, Controller>(Controller());
  162 + final instance = Get.find<DisposableController>();
  163 + expect(instance is Controller, isTrue);
  164 + expect((instance as Controller).init, greaterThan(0));
  165 + });
158 } 166 }
159 167
160 class Controller extends DisposableController { 168 class Controller extends DisposableController {