[英]Flutter MultiProvider Not found
Error: Could not find the correct Provider above this AuthenticationWrapper Widget错误:在此 AuthenticationWrapper 小部件上方找不到正确的提供程序
This happens because you used a BuildContext
that does not include the provider of your choice.发生这种情况是因为您使用的
BuildContext
不包括您选择的提供者。 There are a few common scenarios:有几种常见的场景:
You added a new provider in your main.dart
and performed a hot-reload.您在
main.dart
中添加了一个新提供程序并执行了热重载。 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.您使用的
BuildContext
是您尝试读取的提供程序的祖先。
Make sure that AuthenticationWrapper is under your MultiProvider/Provider.确保 AuthenticationWrapper 在您的 MultiProvider/Provider 下。 This usually happens when you are creating a provider and trying to read it immediately
这通常发生在您创建提供程序并尝试立即读取它时
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:park_app/app_styles.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import './views/pages.dart';
import 'views/authentication/authentication_service.dart';
import 'Home_Page.dart';
bool? seenOnboard;
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
// to show status bar
SystemChrome.setEnabledSystemUIOverlays(
[SystemUiOverlay.bottom, SystemUiOverlay.top]);
// to load onboard for the first time only
SharedPreferences pref = await SharedPreferences.getInstance();
seenOnboard = pref.getBool('seenOnboard') ?? false; //if null set to false
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<AuthenticationService>(
create: (_) => AuthenticationService(FirebaseAuth.instance),
),
StreamProvider(
initialData: null,
create: (context) =>
context.read<AuthenticationService>().authStateChanges),
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Park App',
theme: ThemeData(
textTheme: GoogleFonts.manropeTextTheme(
Theme.of(context).textTheme,
),
primarySwatch: Colors.blue,
scaffoldBackgroundColor: kScaffoldBackground,
),
home: seenOnboard == true ? AuthenticationWrapper() : OnBoardingPage(),
),
);
}
}
class AuthenticationWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final firebaseUser = context.watch<User>();
if (firebaseUser != null) {
return HomePage();
}
return LoginPage();
}
}
Make sure to specify the generic type on StreamProvider
:确保在
StreamProvider
上指定泛型类型:
StreamProvider<User?>(
...
)
Note that you have set null
as initialData, so your widget likely needs to handle null
users.请注意,您已将
null
设置为 initialData,因此您的小部件可能需要处理null
用户。 Meaning you need to do:意味着你需要做:
final user = context.watch<User?>()
install provider by running the command below:通过运行以下命令安装提供程序:
flutter pub add provider
and then in your main.dart file, import it import 'package:provider/provider.dart';
flutter pub add provider
,然后在您的 main.dart 文件中,将其import 'package:provider/provider.dart';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.