Removed the assert that prevented from using ChangeNotifierProvider
with notifiers that already had listeners.
-
Breaking: It is no longer possible to define providers where their only difference is the nullability of the exposed value:
MultiProvider( providers: [ Provider<Model>(...), Provider<Model?>(...) ] )
Before, when defining those providers, doing
context.watch<Model>
vscontext.watch<Model?>
would point to a different provider. Now, both will always point to the deepest provider (in this exampleProvider<Model?>
).That fixes issues where a common developer mistake is to define a provider as
Model?
but try to read it asModel
. Previously, that would give aProviderNotFoundException
. Now, this will correctly resolve the value.That also helps large codebase migrate to null-safety progressively, as they would temporarily contain both null-safe and unsound code. Previously, unsound code would be unable to read a provider defined as
Provider<Model?>
, asModel?
is not a valid syntax. With this change, this is now feasible. -
It is now possible to read a provider without throwing a
ProviderNotFoundException
if the provider is optional.To do so, when calling
context.watch
/context.read
, make the generic type nullable. Such that instead of:context.watch<Model>()
which will throw a
ProviderNotFoundException
if no matching providers are found, do:context.watch<Model?>()
which will try to obtain a matching provider. But if none are found,
null
will be returned instead of throwing.
-
Breaking: It is no longer possible to define providers where their only difference is the nullability of the exposed value:
MultiProvider( providers: [ Provider<Model>(...), Provider<Model?>(...) ] )
Before, when defining those providers, doing
context.watch<Model>
vscontext.watch<Model?>
would point to a different provider. Now, both will always point to the deepest provider (in this exampleProvider<Model?>
).That fixes issues where a common developer mistake is to define a provider as
Model?
but try to read it asModel
. Previously, that would give aProviderNotFoundException
. Now, this will correctly resolve the value.That also helps large codebase migrate to null-safety progressively, as they would temporarily contain both null-safe and unsound code. Previously, unsound code would be unable to read a provider defined as
Provider<Model?>
, asModel?
is not a valid syntax. With this change, this is now feasible. -
It is now possible to read a provider without throwing a
ProviderNotFoundException
if the provider is optional.To do so, when calling
context.watch
/context.read
, make the generic type nullable. Such that instead of:context.watch<Model>()
which will throw a
ProviderNotFoundException
if no matching providers are found, do:context.watch<Model?>()
which will try to obtain a matching provider. But if none are found,
null
will be returned instead of throwing.
- Stable, null-safe release.
- pre-release of the mechanism for state-inspection using the Flutter devtool
- Updated oudated doc in
StreamProvider
Fixed an issue where providers with an update
parameter in sound null-safety mode could throw null exceptions.
- Upgraded
nested
dependency to 1.0.0 andcollection
to 1.15.0
- Improved the error message of
ProviderNotFoundException
to mention hot-reload. (#595) - Removed the asserts that prevented
ChangeNotifier
s inChangeNotifierProvider()
to have listeners (#596) - Removed the opinionated asserts in
context.watch
/context.read
that prevented them to be used inside specific conditions (#585)
- Improved the error message when an exception is thrown inside
create
of a provider`
- Reintroduced
ValueListenableProvider.value
(the default constructor is still removed).
Migrated Provider to non-nullable types:
-
initialData
for bothFutureProvider
andStreamProvider
is now required.To migrate, what used to be:
FutureProvider<int>( create: (context) => Future.value(42), child: MyApp(), ) Widget build(BuildContext context) { final value = context.watch<int>(); return Text('$value'); }
is now:
FutureProvider<int?>( initialValue: null, create: (context) => Future.value(42), child: MyApp(), ) Widget build(BuildContext context) { // be sure to specify the ? in watch<int?> final value = context.watch<int?>(); return Text('$value'); }
-
ValueListenableProvider
is removedTo migrate, you can instead use
Provider
combined withValueListenableBuilder
:ValueListenableBuilder<int>( valueListenable: myValueListenable, builder: (context, value, _) { return Provider<int>.value( value: value, child: MyApp(), ); } )
- Improved the error message of
ProviderNotFoundException
to mention hot-reload. (#595) - Removed the asserts that prevented
ChangeNotifier
s inChangeNotifierProvider()
to have listeners (#596) - Removed the opinionated asserts in
context.watch
/context.read
that prevented them to be used inside specific conditions (#585)
ValueListenableProvider
is no-longer deprecated.
Only its default constructor is deprecated (the .value
constructor is kept)
Marked ValueListenableProvider
as deprecated
Improve pub score
Documentation improvement about the builder
parameter of Providers.
Fixed typo in the error message of ProviderNotFoundException
- Fixed a bug where hot-reload forced all lazy-loaded providers to be computed.
- Added
ReassembleHandler
interface, for objects to implement so thatprovider
let them handle hot-reload.
-
Added a
builder
parameter onMultiProvider
(thanks to @joaomarcos96):MultiProvider( providers: [ ChangeNotifierProvider(create: (ct) => Counter()), ], builder: (ctx, child) { final counter = ctx.watch<Counter>(); return Text('${counter.count}'); }, );
- Small Readme changes
-
Improved the error message of
ProviderNotFoundException
with instructions that better fit what is usually the problem. -
Added documentation on why
context.read
should not be called insidebuild
, and what to do instead. -
Improved the performances of
context.select
, by not calling the selectors when the provider changes if the widgets listening to the value are already needing build. -
Fixes a bug where
context.watch
couldn't be called insideListView
/LayoutBuilder
-
Improve the error message when trying to use
context.select
insideListView.builder
-
Improve the error message when calling
context.read/watch/select
/Provider.of
with acontext
that isnull
.
- Loosened the constraint on Flutter's version to be compatible with
beta
channel.
- Fixes an "aspect" leak with
context.select
, leading to memory leaks and unnecessary rebuilds - Fixes the
builder
parameter of providers not working (thanks to @passsy)
-
Now requires:
- Flutter >= 1.6.0
- Dart >= 2.7.0
-
Added a
select
extension onBuildContext
. It behaves similarly toSelector
, but is a lot less verbose to write:With Selector:
Widget build(BuildContext context) { return Selector<Person, String>( selector: (_, p) => p.name, builder: (_, name, __) { return Text(name); }, ), }
VS with the new
select
extension:Widget build(BuildContext context) { final name = context.select((Person p) => p.name); return Text(name); }
-
Added
builder
on the different providers. This parameter simplifies situations where we need a BuildContext that can access the new provider.As such, instead of:
Provider( create: (_) => Something(), child: Builder( builder: (context) { final name = context.select((Something s) => s.name); return Text(name); }, ), )
we can write:
Provider( create: (_) => Something(), builder: (context, child) { final name = context.select((Something s) => s.name); return Text(name); }, )
The behavior is the same. This is only a small syntax sugar.
-
Added a two extensions on BuildContext, to slightly reduce the boilerplate:
before after Provider.of<T>(context, listen: false)
context.read<T>()
Provider.of<T>(context)
context.watch<T>
-
Added a
Locator
typedef and an extension on BuildContext, to help with being able to read providers from a class that doesn't depend on Flutter.
- Added Português translation of the readme file (thanks to @robsonsilv4)
- Improve error message when forgetting to pass a
child
when using a provider outside ofMultiProvider
(thanks to @felangel)
- Update the ProviderNotFoundException to remove outdated solution. (thanks @augustinreille)
- improved error message when
Provider.of
is called without specifyinglisten: false
outside of the widget tree.
- fix
Provider.of
returning the previous value instead of the new value if called insidedidChangeDependencies
. - fixed an issue where
update
was unnecessarily called.
- stable release of 4.0.0-hotfix+1
- fix some typos
-
removed the inference of the
listen
flag ofProvider.of
in favor of an exception in debug mode iflisten
is true when it shouldn't.This is because it caused a critical performance issue. See rrousselGit#305
Selector
now deeply compares collections by default, and offers ashouldRebuild
to customize the rebuild behavior.- renamed
ProviderNotFoundError
toProviderNotFoundException
. This allows callingProvider.of
inside atry/catch
without triggering a warning. - update provider to work with Flutter 1.12.1
- The creation and listening of objects using providers is now performed lazily. This means that objects are created the first time the value is read instead of the first time the provider is mounted.
Theremoved by 4.0.0-hotfix. See rrousselGit#305listen
argument ofProvider.of
is now automatically inferred. It is no longer necessary to passlisten: false
when callingProvider.of
outside of the widget tree.- renamed
initialBuilder
&builder
of*ProxyProvider
tocreate
&update
- renamed
builder
of*Provider
tocreate
- added a
*ProxyProvider0
variant
- Deprecated "builder" of providers in favor to "create"
- Deprecated "initialBuilder"/"builder" of proxy providers in favor of respectively "create" and "update"
-
Added
Selector
, similar toConsumer
but can filter unneeded updates -
improved the overall documentation
-
fixed a bug where
ChangeNotifierProvider.value
didn't update dependents when theChangeNotifier
instance changed. -
Consumer
can now be used insideMultiProvider
MultiProvider( providers: [ Provider(builder: (_) => Foo()), Consumer<Foo>( builder: (context, foo, child) => Provider.value(value: foo.bar, child: child), ) ], );
Provider
now throws if used with aListenable
/Stream
. This can be disabled by settingProvider.debugCheckInvalidValueType
tonull
.- The default constructor of
StreamProvider
has now builds aStream
instead ofStreamController
. The previous behavior has been moved toStreamProvider.controller
. - All
XXProvider.value
constructors now usevalue
as parameter name. - Added
FutureProvider
, which takes a future and updates dependents when the future completes. - Providers can no longer be instantiated using
const
constructors.
- Added
ProxyProvider
,ListenableProxyProvider
, andChangeNotifierProxyProvider
. These providers allows building values that depends on other providers, without loosing reactivity or manually handling the state. - Added
DelegateWidget
and a few related classes to help building custom providers. - Exposed the internal generic
InheritedWidget
to help building custom providers.
- fix a bug where
ListenableProvider.value
/ChangeNotifierProvider.value
/StreamProvider.value
/ValueListenableProvider.value
subscribed/unsubscribed to their respective object too often - fix a bug where
ListenableProvider.value
/ChangeNotifierProvider.value
may rebuild too often or skip some.
Consumer
now takes an optionalchild
argument for optimization purposes.- merged
Provider
andStatefulProvider
- added a "builder" constructor to
ValueListenableProvider
- normalized providers constructors such that the default constructor is a "builder",
and offer a
value
named constructor.
Provider.of<T>
now crashes with aProviderNotFoundException
when noProvider<T>
are found in the ancestors of the context used.
- new:
ChangeNotifierProvider
, similar to scoped_model that exposesChangeNotifer
subclass and rebuilds dependents only whennotifyListeners
is called. - new:
ValueListenableProvider
, a provider that rebuilds whenever the value passed to aValueNotifier
change.
- new: Add
Consumer
with up to 6 parameters. - new:
MultiProvider
, a provider that makes a tree of provider more readable - new:
StreamProvider
, a stream that exposes to its descendants the current value of aStream
.
- Reintroduced
StatefulProvider
with a modified prototype. The second argument ofvalueBuilder
anddidChangeDependencies
have been removed. AndvalueBuilder
is now called only once for the whole life-cycle ofStatefulProvider
.
- Added
Consumer
, useful when we need to both expose and consume a value simultaneously.
- Added:
HookProvider
, aProvider
that creates its value from aHook
. - Deprecated
StatefulProvider
. Either make aStatefulWidget
or useHookProvider
. - Integrated the widget inspector, so that
Provider
widget shows the current value.
- add
didChangeDependencies
callback to allow updating the value based on anInheritedWidget
- add
updateShouldNotify
method to bothProvider
andStatefulProvider
onDispose
has been added toStatefulProvider
- BuildContext is now passed to
valueBuilder
callback