Skip to content

Commit

Permalink
docs: change selector example from tuple to records (#855)
Browse files Browse the repository at this point in the history
* docs: change selector example from tuple to records

* docs: add more example for selector

* Apply suggestions from code review

---------

Co-authored-by: Remi Rousselet <[email protected]>
  • Loading branch information
GameOnAnil and rrousselGit authored Jan 13, 2024
1 parent ad975c0 commit d696f6e
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions packages/provider/lib/src/selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,37 @@ class _Selector0State<T> extends SingleChildState<Selector0<T>> {
/// As such, it `selector` should return either a collection ([List]/[Map]/[Set]/[Iterable])
/// or a class that override `==`.
///
/// Here's an example:
///
/// Example 1:
///
///```dart
/// Selector<Foo, Bar>(
/// selector: (_, foo) => foo.bar, // will rebuild only when `bar` changes
/// builder: (_, data, __) {
/// return Text('${data.item}');
/// }
/// )
///```
///In this example `builder` will be called only when `foo.bar` changes.
///
/// Example 2:
///
/// To select multiple values without having to write a class that implements `==`,
/// the easiest solution is to use a "Tuple" from [tuple](https://pub.dev/packages/tuple):
/// the easiest solution is to use "Records," available from Dart version 3.0.
/// For more information on Records, refer to the [records](https://dart.dev/language/records).
///
/// ```dart
/// Selector<Foo, Tuple2<Bar, Baz>>(
/// selector: (_, foo) => Tuple2(foo.bar, foo.baz),
/// Selector<Foo, ({String item1, String item2})>(
/// selector: (_, foo) => (item1: foo.item1, item2: foo.item2),
/// builder: (_, data, __) {
/// return Text('${data.item1} ${data.item2}');
/// }
/// )
/// },
/// );
/// ```
///
/// In that example, `builder` will be called again only if `foo.bar` or
/// `foo.baz` changes.
/// In this example, `builder` will be called again only if `foo.item1` or
/// `foo.item2` changes.
///
/// For generic usage information, see [Consumer].
/// {@endtemplate}
Expand Down

0 comments on commit d696f6e

Please sign in to comment.