Provider: Using MultiProvider

Created on 30 Mar 2020  ยท  5Comments  ยท  Source: rrousselGit/provider

Hi, why do I have the following error when I use the MultiProvider?

I/flutter (12486): โ•โ•โ•ก EXCEPTION CAUGHT BY WIDGETS LIBRARY โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
I/flutter (12486): The following assertion was thrown building MyHomePage(dirty, dependencies:
I/flutter (12486): [_DefaultInheritedProviderScope]):
I/flutter (12486): Tried to use Provider with a subtype of Listenable/Stream (A).
I/flutter (12486):
I/flutter (12486): This is likely a mistake, as Provider will not automatically update dependents
I/flutter (12486): when A is updated. Instead, consider changing Provider for more specific
I/flutter (12486): implementation that handles the update mechanism, such as:
I/flutter (12486):
I/flutter (12486): - ListenableProvider
I/flutter (12486): - ChangeNotifierProvider
I/flutter (12486): - ValueListenableProvider
I/flutter (12486): - StreamProvider
I/flutter (12486):
I/flutter (12486): Alternatively, if you are making your own provider, consider using InheritedProvider.

`void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider
(create: (_) => A()),
],
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
),
);
}
}

class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final provider = Provider.of
(context);

return Scaffold(
  appBar: AppBar(
    title: Text('Test'),
  ),
  body: Text(
    'test',
  ),
);

}
}

class A with ChangeNotifier {}`

Most helpful comment

The exception clearly states what is happening.

You used Provider when you likely wanted to use ChangeNotifierProvider

All 5 comments

+1
But I also see you missed the type.
Type t = Provider.of<T>(context);

The exception clearly states what is happening.

You used Provider when you likely wanted to use ChangeNotifierProvider

Sorry, but I don't understund.
If I insert single Provider like this:
return ChangeNotifierProvider( create: (_) => A(), child: ...

It works, but with MultiProvider, I have that message.
Why I have to use ChangeNotifierProvider? The documentation tell me tu use Provider:
MultiProvider( providers: [ Provider<Something>(create: (_) => Something()), Provider<SomethingElse>(create: (_) => SomethingElse()), Provider<AnotherThing>(create: (_) => AnotherThing()), ], child: someWidget, )

The type of provider that you want to use depends on what the object you want to provide is.

ChangeNotifiers uses ChangeNotifierProvider
Streams uses StreamProvider
And so on

Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FaizanKamal7 picture FaizanKamal7  ยท  6Comments

tmxdyf picture tmxdyf  ยท  3Comments

sanekyy picture sanekyy  ยท  5Comments

dikir picture dikir  ยท  5Comments

ykaito21 picture ykaito21  ยท  3Comments