-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Expose ImmutableProvider and ImmutableProviderTree #364
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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 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 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 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 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,8 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
/// A mixin on `Widget` which exposes a `copyWith` method. | ||
mixin Copyable on Widget { | ||
/// `copyWith` takes a child `Widget` and must create a copy of itself with the new child. | ||
/// All values except child (including [Key]) should be preserved. | ||
Widget copyWith(Widget child); | ||
} |
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,59 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'copyable.dart'; | ||
|
||
/// A Flutter widget which provides an immutable value to its children via `ImmutableProvider.of(context)`. | ||
/// It is used as a DI widget so that a single instance of an immutable value can be provided | ||
/// to multiple widgets within a subtree. | ||
class ImmutableProvider<T> extends InheritedWidget with Copyable { | ||
/// The immutable value which will be made availablee to the subtree | ||
final T value; | ||
|
||
const ImmutableProvider({ | ||
Key key, | ||
@required this.value, | ||
Widget child, | ||
}) : super(key: key, child: child); | ||
|
||
@override | ||
bool updateShouldNotify(InheritedWidget _) => false; | ||
|
||
/// Necessary to obtain generic [Type] | ||
/// https://github.com/dart-lang/sdk/issues/11923 | ||
static Type _typeOf<T>() => T; | ||
|
||
/// Method that allows widgets to access the value as long as their `BuildContext` | ||
/// contains an `ImmutableProvider` instance. | ||
static T of<T>(BuildContext context) { | ||
final type = _typeOf<ImmutableProvider<T>>(); | ||
final provider = context | ||
.ancestorInheritedElementForWidgetOfExactType(type) | ||
?.widget as ImmutableProvider<T>; | ||
|
||
if (provider == null) { | ||
throw FlutterError( | ||
""" | ||
ImmutableProvider.of() called with a context that does not contain a value of type $T. | ||
No ancestor could be found starting from the context that was passed to ImmutableProvider.of<$T>(). | ||
This can happen if the context you use comes from a widget above the ImmutableProvider. | ||
This can also happen if you used ImmutableProviderTree and didn\'t explicity provide | ||
the ImmutableProvider types: ImmutableProvider(value: $T()) instead of ImmutableProvider<$T>(value: $T()). | ||
The context used was: $context | ||
""", | ||
); | ||
} | ||
|
||
return provider.value; | ||
} | ||
|
||
/// Clones the current [ImmutableProvider] with a new child [Widget]. | ||
/// All other values, including `key` and `value` are preserved. | ||
@override | ||
ImmutableProvider<T> copyWith(Widget child) { | ||
return ImmutableProvider<T>( | ||
key: key, | ||
value: value, | ||
child: child, | ||
); | ||
} | ||
} |
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.
Good job on this error message. This will help a lot to troubleshoot issues ;)