Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After reset, GetIt returns the same instance of lazy singleton with on going dispose #289

Closed
dzziwny opened this issue Jul 14, 2022 · 0 comments

Comments

@dzziwny
Copy link

dzziwny commented Jul 14, 2022

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();
                                  }),
                            ],
                          );
                        },
                      )),
                    ),
                  ),
                );
              },
            );
          }),
        ),
      ),
    );
  }
}

@dzziwny dzziwny closed this as completed Jul 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant