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 {}`
+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
Most helpful comment
The exception clearly states what is happening.
You used
Providerwhen you likely wanted to useChangeNotifierProvider