简体   繁体   English

Flutter - 找不到正确的提供程序<provider>在此 ChangeLocation 小部件上方</provider>

[英]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:-应用程序的流程在这里: -

  1. first time user opens the app: InstructionPage() -> WelcomePage() -> HomePage() //getting error第一次用户打开应用程序: InstructionPage() -> WelcomePage() -> HomePage() //得到错误
  2. second time user opens the app: HomePage() //working fine第二次用户打开应用程序: HomePage() //工作正常

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.

相关问题 在此小部件上方找不到正确的提供程序 - Flutter - Could not find the correct Provider above this Widget - Flutter 颤振提供者错误:在此小部件上方找不到正确的提供者 - flutter provider error : Could not find the correct provider above this widget Flutter 中的提供程序出现错误“无法在此 X Widget 上方找到正确的提供程序” - Provider in Flutter with error "Could not find the correct provider above this X Widget" Flutter 错误:找不到正确的提供程序<user>在此 Wrapper Widget 上方</user> - Flutter Error: Could not find the correct Provider<User> above this Wrapper Widget Flutter:找不到正确的提供程序<entryprovider>在此页面小部件上方</entryprovider> - Flutter : Could not find the correct Provider<EntryProvider> above this Page Widget Flutter 找不到正确的提供程序<locationprovider>高于此消费者<locationprovider>小部件</locationprovider></locationprovider> - Flutter Could not find the correct Provider<LocationProvider> above this Consumer<LocationProvider> Widget Flutter 错误:找不到正确的提供程序<cart>在此 ProductLandingPage 小部件上方</cart> - Flutter Error : Could not find the correct Provider<Cart> above this ProductLandingPage Widget 错误:在此小部件 Flutter [PropertiesGrid] 上方找不到正确的提供程序 - Error: Could not find the correct Provider above this widget Flutter [PropertiesGrid] Flutter/Dart 在这个 Widget 上方找不到正确的 Provider&lt;&gt; - Flutter/Dart Could not find correct Provider<> above this Widget Flutter 在 BlocConsumer Widget 上方找不到正确的 Provider - Flutter could not find the correct Provider above BlocConsumer Widget
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM