Asyncvalue guard Method inside build Riverpod

  Kiến thức lập trình

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

Mazhar Salam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT