Hi
The resetLazySingleton, won't give a new instance until the previous one is disposed.
I don't see a point why would i need an instance with on going dispose.
I thing, that resetLazySingleton should be completed, once the instance is disposed (that works fine), and during this time, GetIt.I<Service>() should return new instance.
I've already created PR with test and fix, take a look on it -> #284.
Try this sample (go few times back and forth):
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:rxdart/rxdart.dart';
class Service {
final state = BehaviorSubject.seeded('Ready');
Completer? disposeCompleter;
Future<void> dispose() async {
if (disposeCompleter != null) {
await disposeCompleter!.future;
return;
}
disposeCompleter = Completer();
await Stream.fromIterable(Iterable<int>.generate(1000).toList().reversed)
.interval(const Duration(milliseconds: 1))
.map((interval) {
state.add('Will be disposed in: ($interval)');
}).last;
state.add('Disposed');
disposeCompleter!.complete();
}
}
void main() {
GetIt.I.registerLazySingleton<Service>(
() => Service(),
dispose: (service) => service.dispose(),
);
runApp(const App());
}
class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Builder(builder: (context) {
return ElevatedButton(
child: const Text('Go to service'),
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Scaffold(
body: Center(
child: StreamBuilder<String>(
stream: GetIt.I<Service>().state,
builder: (context, snapshot) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Service state: '${snapshot.data}'"),
ElevatedButton(
child: const Text('Back'),
onPressed: () {
GetIt.I.resetLazySingleton<Service>();
Navigator.of(context).pop();
}),
],
);
},
)),
),
),
);
},
);
}),
),
),
);
}
}
Hi
The
resetLazySingleton, won't give a new instance until the previous one is disposed.I don't see a point why would i need an instance with on going dispose.
I thing, that
resetLazySingletonshould be completed, once the instance is disposed (that works fine), and during this time,GetIt.I<Service>()should return new instance.I've already created PR with test and fix, take a look on it -> #284.
Try this sample (go few times back and forth):