rrousselGit / riverpod

A reactive caching and data-binding framework. Riverpod makes working with asynchronous code a breeze.
https://riverpod.dev
MIT License
5.82k stars 888 forks source link

Fixed `AsyncValue.guard` documentation #3440

Open drown0315 opened 1 month ago

drown0315 commented 1 month ago

Fixed the usage of asyncValue.guard in the documentation

Before:

  ///   Future<void> sideEffect() async {
  ///     state = const AsyncValue.loading();
  ///     try {
  ///       final response = await dio.get('my_api/data');
  ///       final data = MyData.fromJson(response);
  ///       state = AsyncValue.data(data);
  ///     } catch (err, stack) {
  ///       state = AsyncValue.error(err, stack);
  ///     }
  ///   }
  /// }

After:

  ///   Future<void> sideEffect() async {
  ///     state = const AsyncValue.loading();
  ///     try {
  ///       final response = await dio.get('my_api/data');
  ///       final data = MyData.fromJson(response);
  ///       if(mounted){
  ///          state = AsyncValue.data(data);
  ///       }
  ///     } catch (err, stack) {
  ///       if(mounted){
  ///          state = AsyncValue.error(err, stack);
  ///       }
  ///     }
  ///   }
  /// }

Because provider may be disposed after await, add code to check if mounted.

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]).

changeset-bot[bot] commented 1 month ago

⚠️ No Changeset found

Latest commit: 2774a9bc91339d8b60ff2cd1d15bebedeeb14fa8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR