-
Notifications
You must be signed in to change notification settings - Fork 6
[listen] Creates listen package #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chunhtai
wants to merge
109
commits into
flutter:main
Choose a base branch
from
chunhtai:create-listen-package
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,648
−0
Open
Changes from all commits
Commits
Show all changes
109 commits
Select commit
Hold shift + click to select a range
753f2b5
PaginatedDataTable (part 1) (#4306)
Hixie ddb441d
Improve change notifier (#4747)
abarth 70d66bc
Extract a Listenable base class from Animation and ChangeNotifier (#5…
abarth b928281
Listenable.merge (#7256)
Hixie 23dda28
Allow nulls in `Listenable.merge` (#7355)
Hixie 5a09f6c
Fix several minor bugs and add many tests (#7506)
abarth ed19257
Add more asserts and docs to ChangeNotifier. (#7513)
Hixie 8659b4b
Strengthen animation listener iteration patterns (#7537)
abarth 9be184a
Revert "Strengthen animation listener iteration patterns" (#7552)
abarth b3b3ff8
Strengthen animation listener iteration patterns (#7566)
abarth f758e93
Various documentation fixes. (#7726)
Hixie 4adb1f3
Add ValueNotifier (#8463)
abarth 3a4717c
Animation Demo performance tweaks (#8586)
cea97ac
Remove imports of meta (#9081)
xster aa8474e
Change foundation references in foundation to meta (#9107)
xster bd82d72
Rationalize text input widgets (#9119)
abarth 5b6a595
Better toStrings for Listenable subclasses (#9244)
Hixie 75fd887
Simplify change notifier toString and handle nulls (#9368)
xster 2521f2e
More docs. (#10214)
Hixie 1d927b5
More documentation (#10589)
Hixie fbc785f
Change all ocurrences of '$runtimeType#$hashCode' to use the idAndTyp…
jacob314 f3fe60a
Revert "Change all ocurrences of '$runtimeType#$hashCode' to use the …
jacob314 0370c2c
Change all occurrences of '$runtimeType#$hashCode' to use the describ…
jacob314 bd4b999
Add Diagnosticable base class and add documentation. (#11458)
jacob314 02e2d32
use bool in assert (#12170)
a14n 19a9b1b
Fix spelling errors in all the dartdocs. (#13061)
gspencergoog 8aa7aca
Fix the confusing-zero case with NestedScrollView. (#14133)
Hixie f9d12c6
some whitespace cleanup (#14443)
a14n c8ae2f8
Add a hasListeners to ChangeNotifier (#14946)
Hixie e0105b9
Unnecessary new (#20138)
a14n 84440f2
[H] Created a variant of InheritedWidget specifically for Listenables…
Hixie 8b034f2
Allow ChangeNotifier to be mixed in again (#23631)
Hixie 3cd601c
make see also sections uniform (#25513)
a14n 003ce83
Fix Listenable.merge to not leak (#26313)
goderbauer 2dca3d3
Remove all obsolete "// ignore:" (#27271)
goderbauer e3335e8
[H] Add ImageStreamCompleter.hasListeners (and cleanup) (#25865)
Hixie e75fc4e
Add missing trailing commas (#28673)
a14n 407052a
fix block formatting (#29051)
a14n 592b64b
some spaces formatting (#29452)
a14n bd2c3ff
Be more explicit when ValueNotifier notifies (#30461)
goderbauer 1064ef9
Refactor core uses of FlutterError. (#30983)
jacob314 599a359
Enable web foundation tests (#34032)
a1dc2dd
fix some bad indentations (#41172)
a14n 5f22f52
Add more structure to errors (continuation of #34684) (#42640)
075b144
License update (#45373)
Hixie 46c3bbe
enable lint prefer_final_in_for_each (#47724)
a14n 120772a
Avoid using FlutterError.fromParts when possible (#43696)
albertusdev 2c68313
Add sample for InheritedNotifier, convert two others to DartPa… (#52349)
gspencergoog 4ee3d62
Revise Action API (#42940)
gspencergoog 18139a9
Skip Audits (2) (#53837)
Piinks 4ef2c3b
Opt out nnbd in packages/flutter (#59186)
a14n 2180621
migrate foundation to nullsafety (#61188)
a14n d9976c4
Minor doc updates (#62008)
tvolkert 2538ba6
[null-safety] update to several framework test cases/APIs for null as…
9e3eaf5
Use a LinkedList to improve the performance of ChangeNotifier (#62330)
rrousselGit caaf888
Migrate foundation test to nullsafety (#62616)
dkwingsmt 049ecbd
Mark keys that match a shortcut, but have no action defined as "not h…
gspencergoog af72157
enable prefer_function_declarations_over_variables lint (#77398)
goderbauer 70a1d53
add missing trailing commas (#79299)
a14n bcfd499
Improve the performances of ChangeNotifier (#71947)
letsar 77d3dc2
Treat some exceptions as unhandled when a debugger is attached (#78649)
goderbauer f476d22
add trailing commas in flutter/test/{foundation,gestures} (#80926)
a14n ae484e4
Enable unnecessary_null_checks lint (#82084)
Abhishek01039 8920924
Add space before curly parentheses. (#85306)
asashour 55352a4
Ban sync*/async* from user facing code (#95050)
dnfield 0867a08
Enable no_leading_underscores_for_local_identifiers (#96422)
goderbauer cf527c8
Allow remove listener on disposed change notifier (#97988)
chunhtai 00c6aee
Add explanation to ChangeNotifier (#98295)
chunhtai ad97e24
feat: Added docstring examples to AnimatedBuilder and ChangeNotifier …
albertodev01 f41901a
Fix `deprecated_new_in_comment_reference` for `material` library (#10…
guidezpl 752591f
super parameters for framework (#100905)
goderbauer c95bf13
Add ShortcutsRegistry (#103456)
gspencergoog c0d40af
Use `curly_braces_in_flow_control_structures` for `foundation`, `gest…
guidezpl 7317aae
Switch debugAssertNotDisposed to be a static (#104772)
gspencergoog badb5ec
Export public API types from foundation/*.dart library. (#105648)
a14n 1351c01
Export public API types from foundation/scheduler/gestures/semantics …
a14n 1e6f91e
Update docs on ChangeNotifier.dispose and KeepAliveHandle.release (#1…
dnfield 11835bb
Can call ChangeNotifier.hasListeners after disposed (#108931)
chunhtai e114036
Create class MemoryAllocations. (#110230)
polina-c 856914a
Fix references to symbols to use brackets instead of backticks (#111331)
gspencergoog 2de98b9
Fix performance regression. (#111615)
polina-c 1179330
Instrument State, Layer, RenderObject and Element. (#111328)
polina-c ddf33c6
Disallow dispose during listener callback (#114530)
chunhtai a55eb7c
Add ListenableBuilder with examples (#116543)
gspencergoog 52c3962
Bump lower Dart SDK constraints to 3.0 & add class modifiers (#122546)
goderbauer 32cf983
Documentation improvements (#122787)
Hixie 550efcb
Define testWidgetsWithLeakTracking. (#125063)
polina-c 0977b2a
Add spaces after flow control statements (#126320)
tgucio f61d571
Unpin leak_tracker and handle breaking changes in API. (#132352)
polina-c 795dc97
Enable ChangeNotifier clients to dispatch event of object creation in…
polina-c 6b04285
Users of ChangeNotifier should dispatch event of object creation in c…
polina-c a0b459b
Resolve breaking change of adding a method to ChangeNotifier. (#134953)
polina-c bc766be
Revert "Resolve breaking change of adding a method to ChangeNotifier.…
zanderso f0a1d75
Reland Resolve breaking change of adding a method to ChangeNotifier. …
polina-c 98b3ef0
Remove usage of testWidgetsWithLeakTracking. (#140239)
polina-c c9a5ebe
Rename MemoryAllocations to FlutterMemoryAllocations. (#140623)
polina-c 419db80
Allow `Listenable.merge()` to use any iterable (#143675)
nate-thegrate 99f64b9
Docimports for foundation (#151119)
goderbauer 95ac2da
Initial empty repository
chadj-at-google bae7241
Trigger Build (#160476)
jtmcdole 7b46eeb
Auto-format Framework (#160545)
goderbauer 927822b
Clean up leak tracker instrumentation tech debt. (#164070)
polina-c 55ed68c
Flutter test cleanup (#170891)
kevmoo 0a91f20
Bump Dart to 3.8 and reformat (#171703)
Piinks 8386a09
Modernize framework lints (#179089)
Piinks 65e165d
Improve documentation about ValueNotifier's behavior (#179870)
AbdeMohlbi 6cd45de
Update doc in foundation to match the style guide (#181972)
chunhtai a2354b4
Adds listenable package
chunhtai da26a37
addressing comments
chunhtai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,9 @@ | ||
| .children | ||
| .project | ||
| .DS_Store | ||
| packages | ||
| pubspec.lock | ||
| .pub | ||
| .packages | ||
| .dart_tool | ||
| .idea |
This file contains hidden or 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,6 @@ | ||
| # Below is a list of people and organizations that have contributed | ||
| # to the project. Names should be added to the list like so: | ||
| # | ||
| # Name/Organization <email address> | ||
|
|
||
| Google Inc. |
This file contains hidden or 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,3 @@ | ||
| ## 1.0.0-beta.1 | ||
|
|
||
| - Moves source code from `flutter/flutter` to `flutter/core-packages`. |
This file contains hidden or 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,25 @@ | ||
| Copyright 2013 The Flutter Authors | ||
|
|
||
| Redistribution and use in source and binary forms, with or without modification, | ||
| are permitted provided that the following conditions are met: | ||
|
|
||
| * Redistributions of source code must retain the above copyright | ||
| notice, this list of conditions and the following disclaimer. | ||
| * Redistributions in binary form must reproduce the above | ||
| copyright notice, this list of conditions and the following | ||
| disclaimer in the documentation and/or other materials provided | ||
| with the distribution. | ||
| * Neither the name of Google Inc. nor the names of its | ||
| contributors may be used to endorse or promote products derived | ||
| from this software without specific prior written permission. | ||
|
|
||
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
| ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
| (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
| ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains hidden or 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,103 @@ | ||
| <?code-excerpt path-base="example/lib"?> | ||
|
|
||
| # listen | ||
|
|
||
| A package to notify state changes to interested listeners in pure Dart. | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Using ValueNotifier | ||
|
|
||
| `ValueNotifier` wraps a single value and notifies listeners whenever the value changes: | ||
|
|
||
| <?code-excerpt "readme_excerpts.dart (ValueNotifier)"?> | ||
| ```dart | ||
| void valueNotifierExample() { | ||
| final counter = ValueNotifier<int>(0); | ||
|
|
||
| counter.addListener(() { | ||
| print('Value changed: ${counter.value}'); | ||
| }); | ||
|
|
||
| counter.value = 5; // Prints: Value changed: 5 | ||
| counter.value = 10; // Prints: Value changed: 10 | ||
| counter.value = 10; // Does not print because the value is == 10 | ||
|
|
||
| counter.dispose(); | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ### Using ChangeNotifier | ||
|
|
||
| Extend or mix in `ChangeNotifier` to manage state and notify listeners manually: | ||
|
|
||
| <?code-excerpt "readme_excerpts.dart (ChangeNotifierClass)"?> | ||
| ```dart | ||
| /// A [ChangeNotifier] subclass that encapsulates a list of items and notifies | ||
| /// listeners whenever items are added or removed. | ||
| class ItemListNotifier extends ChangeNotifier { | ||
| final List<String> _items = <String>[]; | ||
|
|
||
| /// The unmodifiable list of current items. | ||
| List<String> get items => List<String>.unmodifiable(_items); | ||
|
|
||
| /// Adds an [item] to the list and notifies listeners. | ||
| void addItem(String item) { | ||
| _items.add(item); | ||
| notifyListeners(); | ||
| } | ||
|
|
||
| /// Removes an [item] from the list and notifies listeners if it was present. | ||
| void removeItem(String item) { | ||
| if (_items.remove(item)) { | ||
| notifyListeners(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| Then, listen to changes and update state: | ||
|
|
||
| <?code-excerpt "readme_excerpts.dart (ChangeNotifierUsage)"?> | ||
| ```dart | ||
| void changeNotifierExample() { | ||
| final listNotifier = ItemListNotifier(); | ||
|
|
||
| listNotifier.addListener(() { | ||
| print('Current items: ${listNotifier.items}'); | ||
| }); | ||
|
|
||
| listNotifier.addItem('Apple'); // Prints: Current items: [Apple] | ||
| listNotifier.addItem('Banana'); // Prints: Current items: [Apple, Banana] | ||
| listNotifier.removeItem('Apple'); // Prints: Current items: [Banana] | ||
|
|
||
| listNotifier.dispose(); | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ### Merging listenables | ||
|
|
||
| Use `Listenable.merge` to listen to multiple objects simultaneously: | ||
|
|
||
| <?code-excerpt "readme_excerpts.dart (Merge)"?> | ||
| ```dart | ||
| void mergeExample() { | ||
| final first = ValueNotifier<String>('Hello'); | ||
| final second = ValueNotifier<String>('World'); | ||
|
|
||
| final merged = Listenable.merge(<Listenable>[first, second]); | ||
|
|
||
| merged.addListener(() { | ||
| print('Merged listenable triggered: ${first.value} ${second.value}'); | ||
| }); | ||
|
|
||
| first.value = 'Hi'; // Prints: Merged listenable triggered: Hi World | ||
| second.value = 'Dart'; // Prints: Merged listenable triggered: Hi Dart | ||
|
|
||
| first.dispose(); | ||
| second.dispose(); | ||
| } | ||
| ``` |
This file contains hidden or 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,35 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // ignore_for_file: avoid_print | ||
|
|
||
| import 'package:listen/listen.dart'; | ||
|
|
||
| /// A simple counter that extends [ChangeNotifier] to notify listeners | ||
| /// whenever its value changes. | ||
| class Counter extends ChangeNotifier { | ||
| int _count = 0; | ||
|
|
||
| /// The current count value. | ||
| int get count => _count; | ||
|
|
||
| /// Increments the count by one and notifies listeners. | ||
| void increment() { | ||
| _count++; | ||
| notifyListeners(); | ||
| } | ||
| } | ||
|
|
||
| void main() { | ||
| final counter = Counter(); | ||
|
|
||
| counter.addListener(() { | ||
| print('Counter value changed to: ${counter.count}'); | ||
| }); | ||
|
|
||
| counter.increment(); // Prints: Counter value changed to: 1 | ||
| counter.increment(); // Prints: Counter value changed to: 2 | ||
|
|
||
| counter.dispose(); | ||
| } |
This file contains hidden or 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 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // ignore_for_file: avoid_print | ||
|
|
||
| import 'package:listen/listen.dart'; | ||
|
|
||
| /// A [ChangeNotifier] subclass that encapsulates a list of items and notifies | ||
| /// listeners whenever items are added or removed. | ||
| class ItemListNotifier extends ChangeNotifier { | ||
| final List<String> _items = <String>[]; | ||
|
|
||
| /// The unmodifiable list of current items. | ||
| List<String> get items => List<String>.unmodifiable(_items); | ||
|
|
||
| /// Adds an [item] to the list and notifies listeners. | ||
| void addItem(String item) { | ||
| _items.add(item); | ||
| notifyListeners(); | ||
| } | ||
|
|
||
| /// Removes an [item] from the list and notifies listeners if it was present. | ||
| void removeItem(String item) { | ||
| if (_items.remove(item)) { | ||
| notifyListeners(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void main() { | ||
| final listNotifier = ItemListNotifier(); | ||
|
|
||
| listNotifier.addListener(() { | ||
| print('Current items: ${listNotifier.items}'); | ||
| }); | ||
|
|
||
| listNotifier.addItem('Apple'); // Prints: Current items: [Apple] | ||
| listNotifier.addItem('Banana'); // Prints: Current items: [Apple, Banana] | ||
| listNotifier.removeItem('Apple'); // Prints: Current items: [Banana] | ||
|
|
||
| listNotifier.dispose(); | ||
| } |
This file contains hidden or 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,24 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // ignore_for_file: avoid_print | ||
|
|
||
| import 'package:listen/listen.dart'; | ||
|
|
||
| void main() { | ||
| final first = ValueNotifier<String>('Hello'); | ||
| final second = ValueNotifier<String>('World'); | ||
|
|
||
| final merged = Listenable.merge(<Listenable>[first, second]); | ||
|
|
||
| merged.addListener(() { | ||
| print('Merged listenable triggered: ${first.value} ${second.value}'); | ||
| }); | ||
|
|
||
| first.value = 'Hi'; // Prints: Merged listenable triggered: Hi World | ||
| second.value = 'Dart'; // Prints: Merged listenable triggered: Hi Dart | ||
|
|
||
| first.dispose(); | ||
| second.dispose(); | ||
| } |
This file contains hidden or 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,20 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // ignore_for_file: avoid_print | ||
|
|
||
| import 'package:listen/listen.dart'; | ||
|
|
||
| void main() { | ||
| final counter = ValueNotifier<int>(0); | ||
|
|
||
| counter.addListener(() { | ||
| print('Counter changed to: ${counter.value}'); | ||
| }); | ||
|
|
||
| counter.value = 1; // Prints: Counter changed to: 1 | ||
| counter.value = 2; // Prints: Counter changed to: 2 | ||
|
|
||
| counter.dispose(); | ||
| } |
This file contains hidden or 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,91 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // This file exists solely to host compiled excerpts for README.md, and is not | ||
| // intended for use as an actual example application. | ||
|
|
||
| // ignore_for_file: avoid_print | ||
|
|
||
| import 'package:listen/listen.dart'; | ||
|
|
||
| /// Demonstrates [ValueNotifier] usage for README. | ||
| // #docregion ValueNotifier | ||
| void valueNotifierExample() { | ||
| final counter = ValueNotifier<int>(0); | ||
|
|
||
| counter.addListener(() { | ||
| print('Value changed: ${counter.value}'); | ||
| }); | ||
|
|
||
| counter.value = 5; // Prints: Value changed: 5 | ||
| counter.value = 10; // Prints: Value changed: 10 | ||
| counter.value = 10; // Does not print because the value is == 10 | ||
|
|
||
| counter.dispose(); | ||
| } | ||
|
|
||
| // #enddocregion ValueNotifier | ||
|
|
||
| // #docregion ChangeNotifierClass | ||
| /// A [ChangeNotifier] subclass that encapsulates a list of items and notifies | ||
| /// listeners whenever items are added or removed. | ||
| class ItemListNotifier extends ChangeNotifier { | ||
| final List<String> _items = <String>[]; | ||
|
|
||
| /// The unmodifiable list of current items. | ||
| List<String> get items => List<String>.unmodifiable(_items); | ||
|
|
||
| /// Adds an [item] to the list and notifies listeners. | ||
| void addItem(String item) { | ||
| _items.add(item); | ||
| notifyListeners(); | ||
| } | ||
|
|
||
| /// Removes an [item] from the list and notifies listeners if it was present. | ||
| void removeItem(String item) { | ||
| if (_items.remove(item)) { | ||
| notifyListeners(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // #enddocregion ChangeNotifierClass | ||
|
|
||
| /// Demonstrates [ChangeNotifier] usage for README. | ||
| // #docregion ChangeNotifierUsage | ||
| void changeNotifierExample() { | ||
| final listNotifier = ItemListNotifier(); | ||
|
|
||
| listNotifier.addListener(() { | ||
| print('Current items: ${listNotifier.items}'); | ||
| }); | ||
|
|
||
| listNotifier.addItem('Apple'); // Prints: Current items: [Apple] | ||
| listNotifier.addItem('Banana'); // Prints: Current items: [Apple, Banana] | ||
| listNotifier.removeItem('Apple'); // Prints: Current items: [Banana] | ||
|
|
||
| listNotifier.dispose(); | ||
| } | ||
|
|
||
| // #enddocregion ChangeNotifierUsage | ||
|
|
||
| /// Demonstrates [Listenable.merge] usage for README. | ||
| // #docregion Merge | ||
| void mergeExample() { | ||
| final first = ValueNotifier<String>('Hello'); | ||
| final second = ValueNotifier<String>('World'); | ||
|
|
||
| final merged = Listenable.merge(<Listenable>[first, second]); | ||
|
|
||
| merged.addListener(() { | ||
| print('Merged listenable triggered: ${first.value} ${second.value}'); | ||
| }); | ||
|
|
||
| first.value = 'Hi'; // Prints: Merged listenable triggered: Hi World | ||
| second.value = 'Dart'; // Prints: Merged listenable triggered: Hi Dart | ||
|
|
||
| first.dispose(); | ||
| second.dispose(); | ||
| } | ||
| // #enddocregion Merge | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason this is duplicated almost exactly here from
main.dart, instead of the README snippet just pulling frommain.dart?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That will require adding the #docregion identifier on the main.dart, which I think may be confusing for people looking at the example. so I intentionally group all excerpt related code in one file even if I can probably grab the code from some other example. Let me know reducing duplication here is more preferred.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Traditionally we haven't worried about having it in main.dart, and only use readme_excerpts.dart when there were things we wanted to show in the README that weren't in the example.
It's not a rule though, so if you are concerned about the comments in the example this is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will keep this as-is