Skip to content

how to register an instance with generic param #48

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
dungnv2602 opened this issue Feb 16, 2020 · 9 comments
Closed

how to register an instance with generic param #48

dungnv2602 opened this issue Feb 16, 2020 · 9 comments

Comments

@dungnv2602
Copy link

  locator.registerFactory<TmdbBloc<T>>(
    () => TmdbBloc<T>(
      locator<TmdbRepo>(),
    ),
  );

This won't work.

@escamoteur
Copy link
Collaborator

What Error do you get?

@dungnv2602
Copy link
Author

The name 'T' isn't a type so it can't be used as a type argument.
Try correcting the name to an existing type, or defining a type named 'T'.dart(non_type_as_type_argument)

My TmdbBloc is a generic class

@escamoteur
Copy link
Collaborator

can you show me the class this here is from?

  locator.registerFactory<TmdbBloc<T>>(
    () => TmdbBloc<T>(
      locator<TmdbRepo>(),
    ),
  );

@dungnv2602
Copy link
Author

class TmdbPagedBloc<T> extends Bloc<PagedBlocEvent<T>, PagedBlocState<T>> {
  TmdbPagedBloc(TmdbRepo repo) : _repo = repo;

  final TmdbRepo _repo;
  PagedBlocModel<T> _model;
  PagedResult<T> _pagedResult;

  @override
  PagedBlocState<T> get initialState => PagedBlocStateIdle<T>();

  @override
  Stream<PagedBlocState<T>> mapEventToState(
    PagedBlocEvent<T> event,
  ) async* {
    yield* event.map(
      load: load,
      loadMore: loadMore,
      reload: reload,
      reset: reset,
    );
  }
}

Here you go

@escamoteur
Copy link
Collaborator

I don't see the registerFactory anywhere there?

@dungnv2602
Copy link
Author

Of course locator.registerFactory is from a class named setup_locator or something which defines final locator = GetIt.instance;. It's too obvious, I don't think I need to show that.

@escamoteur
Copy link
Collaborator

I just want to know where the parameter T comes from. That's why I wouldd like to see the class where the registerFactory is

@dungnv2602
Copy link
Author

dungnv2602 commented Mar 4, 2020

I don't understand what you mean, T is a generic type, that's it. I just wanna know whether get_it support registering generic type or not?

@escamoteur
Copy link
Collaborator

I'm pretty sure it's not possibe because generics get resolved at compile time while get_it works with runtime types

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