Skip to content

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

@dzziwny

Description

@dzziwny

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions