@@ -28,31 +28,48 @@ class MyService {
28
28
29
29
MyService ._();
30
30
31
- static void register <T extends CustomService >(T Function () createInstance, {List <Type >? dependsOn})
31
+ static void register <T extends Object >(T Function () createInstance, {List <Type >? dependsOn})
32
32
{
33
33
GetIt .instance.registerSingletonAsync <T >(() async
34
34
{
35
35
T instance = createInstance ();
36
36
37
- await instance.initialize ();
37
+ if (T is MyServiceInitializer )
38
+ {
39
+ await (instance as MyServiceInitializer ).initialize ();
40
+ }
38
41
39
42
return instance;
40
43
},
41
44
dispose: (instance) async
42
45
{
46
+ if (instance is MyServiceInitializer )
47
+ {
43
48
await instance.dispose ();
49
+ }
44
50
},
45
51
dependsOn: dependsOn,
46
52
);
47
53
}
48
54
49
- static Future <void > registerAndWaitUntilReady <T extends CustomService >(T Function () createInstance) async
55
+ static Future <void > registerAndWaitUntilReady <T extends Object >(T Function () createInstance) async
50
56
{
51
57
T instance = createInstance ();
52
58
53
- await instance.initialize ();
59
+ if (T is MyServiceInitializer )
60
+ {
61
+ await (instance as MyServiceInitializer ).initialize ();
62
+ }
54
63
55
- GetIt .instance.registerSingleton <T >(instance);
64
+ GetIt .instance.registerSingleton <T >(instance,
65
+ dispose: (instance) async
66
+ {
67
+ if (instance is MyServiceInitializer )
68
+ {
69
+ await instance.dispose ();
70
+ }
71
+ },
72
+ );
56
73
}
57
74
58
75
@@ -127,7 +144,7 @@ class MyService {
127
144
}
128
145
129
146
130
- abstract class CustomService {
147
+ abstract class MyServiceInitializer {
131
148
132
149
Future <void > initialize ();
133
150
Future <void > dispose ();
0 commit comments