Skip to content

How to make my app easily being added new features using GetIt? #219

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
iandis opened this issue Aug 8, 2021 · 1 comment
Closed

How to make my app easily being added new features using GetIt? #219

iandis opened this issue Aug 8, 2021 · 1 comment

Comments

@iandis
Copy link

iandis commented Aug 8, 2021

Hi, thanks for your great library! However I need a way to make my app easily being added new features using GetIt.

For instance, I have an abstract base class BaseMoviesRepository

abstract class BaseMoviesRepository {
  Future<List<Movie>> getMovieListById(List<BaseMovie> movieIds);
  Future<MovieDetail> getMovieDetail(int id);
  Future<List<Movie>> getNowPlaying({int page = 1});
  Future<List<Movie>> getUpcoming({int page = 1});
  Future<List<Movie>> searchMovie(String keyword, {int page = 1});
}

with its implementation

class MoviesRepository implements BaseMoviesRepository {
  final BaseNetworkService _networkService;

  MoviesRepository({
    BaseNetworkService? networkService,
  }) : _networkService = networkService ?? GetIt.I<BaseNetworkService>();
  
  ...

then next time if I need to add a new feature, I don't want to add anything to BaseMoviesRepository, so I create

abstract class BaseMovies2Repository extends BaseMoviesRepository{
  Future<List<Movie>> getPopular({int page = 1});
}

with its implementation

class Movies2Repository extends MoviesRepository implements BaseMovies2Repository {
  @override
  Future<List<Movie>> getPopular({int page = 1}) {
    ...
  }
} 

now suggest that I already have an app that most of its current features depend on BaseMoviesRepository but I need to register BaseMovies2Repository because I need the new feature getPopular.

the problem here is GetIt does not recognize BaseMoviesRepository as a parent class of BaseMovies2Repository, so if I try

GetIt.I.registerSingleton<BaseNetworkService>(NetworkService());
GetIt.I.registerSingleton<BaseMovies2Repository>(Movies2Repository());
final moviesRepo = GetIt.I<BaseMoviesRepository>();

or

GetIt.I.registerSingleton<BaseNetworkService>(NetworkService());
GetIt.I.registerSingleton<BaseMoviesRepository>(Movies2Repository());
final moviesRepo = GetIt.I<BaseMovies2Repository>();

will throw

Failed assertion: line 372 pos 7: 'instanceFactory != null': Object/factory with  type BaseMoviesRepository is not registered inside GetIt. 
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
Did you forget to register it?)

am I missing something here? or do I need to register a new instance instead?

@iandis iandis changed the title How to make my app scalable using GetIt? How to make my app easily being added new features using GetIt? Aug 8, 2021
@iandis
Copy link
Author

iandis commented Aug 14, 2021

solved it by creating an extension that casts the parent class to the child one

extension GetItFromAs on GetIt {
  AS fromAs<FROM extends Object, AS extends FROM>() {
    return call<FROM>() as AS;
  }
}

void main() {
  GetIt.I.registerSingleton<BaseNetworkService>(NetworkService());
  GetIt.I.registerSingleton<BaseMoviesRepository>(Movies2Repository());
  final moviesRepo = GetIt.I.fromAs<BaseMoviesRepository, BaseMovies2Repository>();
  ...
}

@iandis iandis closed this as completed Aug 14, 2021
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