I could not get the constructor logic with the below code block. In the named constructors we use ClassName({this.variable or required this.variable}); But, in the below example the developer choose a different way. It uses initializer list too but could not get the logic why it defines a variable like this
MetaWeatherApiClient? weatherApiClient
in the constructor's body?
class WeatherRepository {
WeatherRepository({
MetaWeatherApiClient? weatherApiClient
}) : _weatherApiClient = weatherApiClient ?? MetaWeatherApiClient();
final MetaWeatherApiClient _weatherApiClient;
Future<Weather> getWeather(String city) async {
final location = await _weatherApiClient.locationSearch(city);
final woeid = location.woeid;
final weather = await _weatherApiClient.getWeather(woeid);
return Weather(
temperature: weather.theTemp,
location: location.title,
condition: weather.weatherStateAbbr.toCondition,
);
}
}