-
-
Notifications
You must be signed in to change notification settings - Fork 929
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17c6dfe
commit 760f4a6
Showing
4 changed files
with
147 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
title: Logging and error reporting | ||
--- | ||
|
||
import { Link } from "../../src/components/Link"; | ||
import { AutoSnippet, When } from "../../src/components/CodeSnippet"; | ||
import providerObserver from "!!raw-loader!./provider_observer/provider_observer.dart"; | ||
import asyncError from "!!raw-loader!./provider_observer/async_error.dart"; | ||
|
||
Riverpod natively offers a way to listen to all events happening | ||
in the provider tree. | ||
This can be used to log all the events, or to report errors to a | ||
remote service. | ||
|
||
This is achieved by using the `ProviderObserver` class, | ||
and passing it to `ProviderScope`/`ProviderContainer`. | ||
|
||
## Defining a ProviderObserver | ||
|
||
A `ProviderObserver` is a class that should be extended. | ||
It offers various methods which can be overridden to listen to events: | ||
|
||
- `didAddProvider`, called when a provider is added to the tree | ||
- `didUpdateProvider`, called when a provider is updated | ||
- `didDisposeProvider`, called when a provider is disposed | ||
- `providerDidFail`, when a synchronous provider throws an error | ||
|
||
<AutoSnippet raw={providerObserver} /> | ||
|
||
:::caution | ||
At the moment, if an error in an asynchronous provider is thrown, | ||
the error will be received by the `didAddProvider`/`didUpdateProvider` methods | ||
instead, with an `AsyncError` as value. | ||
Note that this will be changed in the version 3.0 of Riverpod. | ||
|
||
<AutoSnippet raw={asyncError} /> | ||
|
||
::: | ||
|
||
## Using a ProviderObserver | ||
|
||
Now that we've defined an observer, we need to use it. | ||
To do so, we should pass it to either `ProviderScope` or `ProviderContainer`: | ||
|
||
```dart | ||
runApp( | ||
ProviderScope( | ||
observers: [ | ||
MyObserver(), | ||
], | ||
child: MyApp(), | ||
) | ||
); | ||
``` | ||
|
||
```dart | ||
final container = ProviderContainer( | ||
observers: [ | ||
MyObserver(), | ||
], | ||
); | ||
``` |
41 changes: 41 additions & 0 deletions
41
website/docs/essentials/provider_observer/async_error.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// ignore_for_file: avoid_print | ||
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
||
/* SNIPPET START */ | ||
class MyObserver extends ProviderObserver { | ||
void handleValue(Object? value) { | ||
if (value is AsyncError) { | ||
print('Error: ${value.error}'); | ||
} | ||
} | ||
|
||
@override | ||
void didAddProvider( | ||
ProviderBase<Object?> provider, | ||
Object? value, | ||
ProviderContainer container, | ||
) { | ||
handleValue(value); | ||
} | ||
|
||
@override | ||
void didUpdateProvider( | ||
ProviderBase<Object?> provider, | ||
Object? previousValue, | ||
Object? newValue, | ||
ProviderContainer container, | ||
) { | ||
handleValue(newValue); | ||
} | ||
|
||
@override | ||
void providerDidFail( | ||
ProviderBase<Object?> provider, | ||
Object error, | ||
StackTrace stackTrace, | ||
ProviderContainer container, | ||
) { | ||
print('error: $error'); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
website/docs/essentials/provider_observer/provider_observer.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// ignore_for_file: avoid_print | ||
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
||
/* SNIPPET START */ | ||
class MyObserver extends ProviderObserver { | ||
@override | ||
void didAddProvider( | ||
ProviderBase<Object?> provider, | ||
Object? value, | ||
ProviderContainer container, | ||
) { | ||
print('Provider $provider was initialized with $value'); | ||
} | ||
|
||
@override | ||
void didDisposeProvider( | ||
ProviderBase<Object?> provider, | ||
ProviderContainer container, | ||
) { | ||
print('Provider $provider was disposed'); | ||
} | ||
|
||
@override | ||
void didUpdateProvider( | ||
ProviderBase<Object?> provider, | ||
Object? previousValue, | ||
Object? newValue, | ||
ProviderContainer container, | ||
) { | ||
print('Provider $provider updated from $previousValue to $newValue'); | ||
} | ||
|
||
@override | ||
void providerDidFail( | ||
ProviderBase<Object?> provider, | ||
Object error, | ||
StackTrace stackTrace, | ||
ProviderContainer container, | ||
) { | ||
print('Provider $provider threw $error at $stackTrace'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters