Skip to content

async dispose not work #254

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

Closed
yang-lile opened this issue Feb 12, 2022 · 4 comments
Closed

async dispose not work #254

yang-lile opened this issue Feb 12, 2022 · 4 comments

Comments

@yang-lile
Copy link

example code:

import 'package:get_it/get_it.dart';
import 'package:test/test.dart';

class ClassName {
  Future<ClassName> init() async {
    await Future.delayed(Duration.zero);
    return this;
  }
}

Future<void> main() async {
  GetIt.I.registerSingletonAsync<ClassName>(
    () => ClassName().init(),
    dispose: (param) async {
      await Future.delayed(Duration.zero);
      print('object');
    },
  );

  // group('getit test', () {
  //   test('get I', () async {
  //     await GetIt.I.isReady<ClassName>();
  //     var className = GetIt.I.get<ClassName>();
  //     expect(className is ClassName, true);
  //   });
  // });

  await GetIt.I.isReady<ClassName>();
  var className = GetIt.I.get<ClassName>();
}

expect: terminal output object
actual: nothing output.

How should I do pls!

@escamoteur
Copy link
Collaborator

escamoteur commented Feb 12, 2022 via email

@yang-lile
Copy link
Author

I try it, but I can't find function of dispose called. it quit immediately. I learn from the scope_test.dart, maybe I need called await GetIt.I.resetScope(); to let it dispose.

@escamoteur
Copy link
Collaborator

escamoteur commented Feb 12, 2022 via email

@yang-lile
Copy link
Author

I know How to call the dispose method:

import 'package:get_it/get_it.dart';
import 'package:test/test.dart';

class ClassName {
  int count = 0;
  dispose() {
    count++;
  }
}

Future<void> main() async {
  GetIt.I.registerSingleton<ClassName>(
    ClassName(),
    dispose: (param) async {
      await Future.delayed(Duration.zero);
      param.dispose();
    },
  );

  group('getit test', () {
    test('get I', () async {
      var className = GetIt.I.get<ClassName>();
      expect(className.count, 0);

      await GetIt.I.unregister<ClassName>();
      expect(className.count, 1);
    });
  });
}

unregister it.

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

2 participants