[英]Flutter - Could not find the correct Provider<Provider> above this ChangeLocation Widget
I'm using both BlocProvider & ChangeNotifierProvider in my app.我在我的应用程序中同时使用了 BlocProvider 和 ChangeNotifierProvider。 The flow of the app goes here:-
应用程序的流程在这里: -
I'm using sharedPreference to store the value of isInstructionPageLoaded.我使用 sharedPreference 来存储 isInstructionPageLoaded 的值。 But navigating from WelcomePage() to HomePage() getting error Could not find the correct Provider above this ChangeLocation Widget
但是从 WelcomePage() 导航到 HomePage() 出现错误找不到此 ChangeLocation 小部件上方的正确提供程序
here is my code:-这是我的代码:-
//main.dart //main.dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await StorageUtil.getInstance();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: Theme.of(context).copyWith(primaryColor: kBgColorGreen),
home: MultiBlocProvider(
providers: [
BlocProvider(
create: (context) =>
RestaurantBloc()..add(RestaurantPageFetched())),
],
child: MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => LocationServiceProvider()),
],
child: StorageUtil.getBoolValue(
SharedPrefsKeys.isInstructionPageLoaded)
? HomePage()
: InstructionScreen(),
)),
routes: Routes.getRoutes(),
);
}
}
//routes.dart //routes.dart
class Routes {
static const String instruction = '/instruction';
static const String welcome = '/welcome';
static const String home = '/home';
static const String change_location = '/change_location';
static Map<String, WidgetBuilder> getRoutes() {
return {
Routes.instruction: (context) => InstructionScreen(),
Routes.welcome: (context) => WelcomePage(),
Routes.home: (context) => HomePage(),
Routes.change_location: (context) => ChangeLocation(),
};
}
}
//location_service.dart //location_service.dart
class LocationServiceProvider extends ChangeNotifier {
void toogleLocation(LocationService location) {
location.isLocationUpdated = !location.isLocationUpdated;
notifyListeners();
}
}
class LocationService {
bool isLocationUpdated = false;
}
//welcome_page.dart - on button pressed calling below method //welcome_page.dart - 按下按钮调用下面的方法
void _navigateToHomePage() async {
Navigator.push(context, MaterialPageRoute(builder: (context) {
return BlocProvider(
create: (context) => RestaurantBloc()..add(RestaurantPageFetched()),
child: ChangeNotifierProvider(create: (context) => LocationServiceProvider(),
child: HomePage(),),
);
}));
} }
I have added BlocProvider in above method becoz before it was giving me error blocprovider.of() called with a context that does not contain a bloc navigating from other screen from navigating from WelcomePage() to HomePage().我在上面的方法becoz中添加了BlocProvider,然后它给我错误blocprovider.of()调用的上下文不包含从其他屏幕导航的块,从WelcomePage()导航到HomePage()。
Thanks in advance!!!提前致谢!!!
To make sure the blocs are exposed to new routes, you need to follow the documentation and add BlocProvider.value() to provide the value of the bloc to new routes.为确保 bloc 暴露于新路由,您需要遵循文档并添加 BlocProvider.value() 以将 bloc 的值提供给新路由。 This will carry the bloc's state and make your life easier.
这将携带该集团的 state,让您的生活更轻松。
Check the Official Documentations for a clear step-by-step guide;).查看官方文档以获得清晰的分步指南;)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.