Elementary-team / flutter-elementary

This is a home to a family of Elementary library packages.
MIT License
127 stars 43 forks source link

change CountryListScreenModel to use injected CountryRepository #82

Closed pishguy closed 1 year ago

pishguy commented 1 year ago

in Country sample code we have this implementation into main.dart:

_http = Dio();
_defaultErrorHandler = DefaultErrorHandler();
_countryClient = CountryClient(_http);
_countryRepository = CountryRepository(_countryClient);

and ower country_client.dart class is:

@RestApi()
abstract class CountryClient {
  factory CountryClient(Dio dio, {String baseUrl}) = _CountryClient;

  /// Получение списка адресов пользователя
  @GET(AppUrls.all)
  Future<List<CountryData>> getAll();
}

and providers is:

return MultiProvider(
    providers: [
      Provider<CountryListScreenModel>(
        create: (_) => _countryListScreenModel,
      ),
      Provider<ThemeWrapper>(
        create: (_) => _themeWrapper,
      ),
    ],
    child: widget.app,
);

i want to use _countryClient the same as DI with Provider and my problem is when i try to define country_client.dart to use that with ProxyProvider, my CountryListScreenModel class of mine no longer have a constructor parameter named _countryRepository

my RestClient class:

const String _baseApiUrl = 'https://restcountries.com/v3.1';

@RestApi(baseUrl: _baseApiUrl)
abstract class RestClient {
  factory RestClient(Dio dio, {String baseUrl}) = _RestClient;

  @GET('$_baseApiUrl/all')
  Future<List<CountryData>> getAll();

  static RestClient create() {
    final BaseOptions options = BaseOptions(
      connectTimeout:const Duration(seconds: 3000),
      receiveTimeout: const Duration(seconds: 3000),
    );

    /* ... */
    return _RestClient(dio);
  }
}

my rest_client provider:

Provider(
  create: (_) => RestClient.create(),
),

i defined CountryRepository class with ProxyProvider to use RestClient which is defined as above, contrary to what you have defined in the code.

my code:

ProxyProvider<RestClient, CountryRepository>(
  update: (context, api, viewModel) => CountryRepository(restApi: api),
),

your code:

_http = Dio();
_countryClient = CountryClient(_http);
_countryRepository = CountryRepository(_countryClient);

now how can i define CountryListScreenModel with ProxyProvider or other provider to use getAllCountries() function of that? because i don't have implementation the same as :

_countryListScreenModel = CountryListScreenModel(
  _countryRepository,
  _defaultErrorHandler,
);

how can i define CountryRepository into provider such as ProxyProvider to use that into CountryListScreenModel class?

👉👉👉UPDATED GITHUB REPOSITORY👈👈👈