Skip to content

How to use registerFactory ? #101

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
jackyhieu1211-hn opened this issue Aug 5, 2020 · 8 comments
Closed

How to use registerFactory ? #101

jackyhieu1211-hn opened this issue Aug 5, 2020 · 8 comments

Comments

@jackyhieu1211-hn
Copy link

jackyhieu1211-hn commented Aug 5, 2020

Hello

I have 2 class

abstract class UserRepository {
  Future<User> getUser;
}
class UserRepositoryImpl implements UserRepository {
  @override
  Future<User> getUser() {
   // ..... logic
  }
}

I use like below


sl.registerFactory<UserRepository>(
     () => UserSummaryRepositoryImpl());

on main.dart

UserRepository _userRepository = sl.get(); 
_userRepository.getUser();

And get error below

No type UserRepositoryImpl is registered inside GetIt.
 Did you forget to pass an instance name? 
(Did you accidentally do  GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;did you forget to register it?)
'package:get_it/get_it_impl.dart':
Failed assertion: line 251 pos 14: 'instanceFactory != null'

NOTE: If I use the following then it works. But it is not clear.

    sl.registerSingleton(UserRepositoryImpl());
    sl.registerFactory<UserRepository>(
            () => sl.get<UserRepositoryImpl>());

Please help me solve the above case. thank you very much

@escamoteur
Copy link
Collaborator

From the code above this seems a bit strange to me. I always recomment passing the Generic type when you use get.
you can then write sl<MyType>() and omit the get.

What I don't understand is this here:

sl.registerFactory<UserRepository>(
     () => UserSummaryRepositoryImpl());

where you use a complete different Implementation than the one in your error Message.
Please create a small repro project on GitHub, so that I can have a closer look

@jackyhieu1211-hn
Copy link
Author

@escamoteur hello sir
I created a sample project
Please check here.
https://github.com/hieuseesaa/test_floor

@escamoteur
Copy link
Collaborator

you mixed up async registration and sync call of get

your function must look like

  static _registerDatabase() async {
    AppDatabase database =
        await $FloorAppDatabase.databaseBuilder('test.db').build();
    sl.registerLazySingleton(() => database.userDao);
  }

@jackyhieu1211-hn
Copy link
Author

@escamoteur thank you. It worked.

Can you help me with this question?

I'm android developer. I used https://github.com/InsertKoinIO/koin to inject function.
for classes UserRepository and UserRepositoryImpl. I just used the following

factory<UserRepository> {
    UserRepositoryImpl()
}

But for get_it. I need to do the following

sl.registerSingleton(UserRepositoryImpl());
sl.registerFactory(() => sl.get());

=====> So for get_it. Is there a way to do the same as koin ?. Please help me. Thank you very much.

@escamoteur
Copy link
Collaborator

What are you trying to do there?
´´´
sl.registerSingleton(UserRepositoryImpl());
sl.registerFactory(() => sl.get());
´´´
I would just do:

sl.registerSingleton<UserRepository>(UserRepositoryImpl());

@jackyhieu1211-hn
Copy link
Author

@escamoteur Thank you very much. It worked.

@jackyhieu1211-hn
Copy link
Author

jackyhieu1211-hn commented Aug 18, 2020

@escamoteur
Hello sir. I have a problem.
This is project example
https://github.com/hieuseesaa/test_floor

When I add UserDao inside UserRepositoryImpl. It show error.

I use like below

class UserRepositoryImpl implements UserRepository {
  UserManager manager;
  UserDAO userDAO;

  UserRepositoryImpl(this.manager, this.userDAO);

  @override
  User getUser() {
    manager.test();
    final id = Random.secure().nextInt(1000);
    print("ID ====> $id");
    return User("$id", "Google");
  }

  @override
  void insert(User user) async {
    userDAO.insertUser(user.toUserEntity());
  }
}
abstract class UserRepository {
  User getUser();
  void insert();
}

GetIt sl = GetIt.instance;

class ServiceLocator {
  static Future<void> initCommon() async {
    sl.registerSingleton(UserManager());
    await _registerDatabase();
  }


  static _registerDatabase() async {
    AppDatabase database =
        await $FloorAppDatabase.databaseBuilder('test.db').build();
    sl.registerLazySingleton(() => database.userDao);
    sl.registerSingleton<UserRepository>(UserRepositoryImpl(sl.get(), sl.get<UserDAO>()));
  }
}

This is error

The following assertion was thrown building Builder:
No type UserRepository is registered inside GetIt.
 Did you forget to pass an instance name? 
(Did you accidentally do  GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
did you forget to register it?)
'package:get_it/get_it_impl.dart':
Failed assertion: line 257 pos 14: 'instanceFactory != null'

Please help me. thank you very much.

@escamoteur
Copy link
Collaborator

You never registered a type `UserRepository´ in your code. You only registered a derived class. If you would always use the generic types that would not happen to you.

Instead of

sl.registerSingleton(UserManager());

you have to do

sl.registerSingleton<UserRepository>(UserManager());

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