I have migrated to dart null safety version. The command in migrate function fixed most issues. However, I have a Stream Provider which handles the User Session using Firebase. After the migration to Provider version 5.0.0 the app is crashing. Below is my main class.
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();
await Firebase.initializeApp();
runApp(EasyLocalization(
child: MyApp(),
path: "assets/langs",
saveLocale: true,
supportedLocales: [
Locale('en', 'US'),
Locale('en', 'GB'),
Locale('es', 'ES'),
],
));
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<AuthenticationProvider>(
create: (_) => AuthenticationProvider(FirebaseAuth.instance),
),
StreamProvider(
create: (context) =>
context.read<AuthenticationProvider>().authState,
initialData: null,
child: Authenticate())
],
child: ScreenUtilInit(
builder: () => MaterialApp(
builder: (context, child) {
return ScrollConfiguration(
//Removes the whole app's scroll glow
behavior: MyBehavior(),
child: child!,
);
},
title: 'SampleApp',
debugShowCheckedModeBanner: false,
theme: theme(),
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
home: Authenticate(),
routes: routes,
),
),
);
}
}
class Authenticate extends StatelessWidget {
@override
Widget build(BuildContext context) {
final firebaseUser = context.watch<User>();
if (firebaseUser != null) {
FirebaseFirestore.instance
.collection('user')
.doc(firebaseUser.uid)
.get()
.then((value) {
UserData.name = value.data()!['name'];
UserData.age = value.data()!['age'];
});
return View1();
}
return View2();
}
}
class MyBehavior extends ScrollBehavior {
@override
Widget buildViewportChrome(
BuildContext context, Widget child, AxisDirection axisDirection) {
return child;
}
}
The app is crashing with the following exception
The following ProviderNotFoundException was thrown building Authenticate(dirty): Error: Could not find the correct Provider above this Authenticate Widget
This happens because you used a BuildContext
that does not include the provider
of your choice. There are a few common scenarios:
You added a new provider in your
main.dart
and performed a hot-reload. To fix, perform a hot-restart.The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then other routes will not be able to access that provider.
You used a
BuildContext
that is an ancestor of the provider you are trying to read.Make sure that Authenticate is under your MultiProvider/Provider. This usually happens when you are creating a provider and trying to read it immediately.