below is a code snippet from a tutorial I am trying to learn.
appreciate if you could tell me why we are not using protection : asyncvalue.guard inside build() but later used when calling the same method?
@riverpod
class WeatherFirst extends _$WeatherFirst {
@override
FutureOr<String> build() {
return _getTemp(Cities.seoul);
}
Future<String> _getTemp(Cities city) async {
await Future.delayed(const Duration(seconds: 1));
switch (city) {
case Cities.seoul:
return '${city.name} - 23';
case Cities.london:
throw 'Fail to fetch the temperature of ${city.name}';
case Cities.bangkok:
throw 'Fail to fetch the temperature of ${city.name}';
case Cities.tokyo:
return '${city.name} - 28';
}
}
Future<void> getTemperature(Cities city) async {
state = const AsyncLoading();
state = await AsyncValue.guard(() => _getTemp(city));
}
}
i tried inserting various error scenarios code works fine
New contributor