Skip to content

Commit

Permalink
Mention lazy initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
rrousselGit committed Jul 25, 2023
1 parent de8bae3 commit 16bbafe
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 7 additions & 5 deletions website/docs/essentials/first_request.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,15 @@ that:
but instead return the previously fetched activity.
- If the UI stops using this provider, the cache will be destroyed.
Then, if the UI ever uses the provider again, that a new network request will be made.
- We did not catch errors. This is voluntary, as providers
natively handle errors.
If the network request or if the JSON parsing throws, the error
will be caught by Riverpod. Then, the UI will automatically have the necessary
information to render an error page.

:::info
Notice how we did not catch errors. This is voluntary, as providers
natively handle errors.
If the network request or if the JSON parsing throws, the error
will be caught by Riverpod. Then, the UI will automatically have the necessary
information to render an error page.
Providers are "lazy". Defining a provider will not execute the network request.
Instead, the network request will be executed when the provider is first read.
:::

### Rendering the network request's response in the UI
Expand Down
2 changes: 2 additions & 0 deletions website/docs/essentials/first_request/raw/consumer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Home extends StatelessWidget {
Widget build(BuildContext context) {
return Consumer(
builder: (context, ref, child) {
// Read the activityProvider. This will start the network request
// if it wasn't already started.
// By using ref.watch, this widget will rebuild whenever the
// the activityProvider updates. This can happen when:
// - The response goes from "loading" to "data/error"
Expand Down

0 comments on commit 16bbafe

Please sign in to comment.