简体   繁体   English

提供了上下文但抛出错误找不到正确的提供者<Data>在这个 MyApp Widget Flutter 之上

[英]context provided but throws error Could not find the correct Provider<Data> above this MyApp Widget Flutter

I am using provider for state management.我正在使用提供程序进行状态管理。 The state variables works fine for other widgets.状态变量适用于其他小部件。 But when I use the state variable for theme in materialApp this throws me the error(restarted too).但是当我在 materialApp 中为主题使用状态变量时,这会引发错误(也重新启动)。 Also declared provider high in the tree.还声明了树中的提供者。

    void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<Data>(
      create: (context) => Data(),
      child: MaterialApp(
        theme: Provider.of<Data>(context).current,
        home: Page1(),
        debugShowCheckedModeBanner: false,
      ),
    );
  }}

The Data Class:数据类:

class Data extends ChangeNotifier {
  String school = "xyz";
  String name = "samuel";
  int age = 123;
  int val = 0;
  String userinput = "";
  ThemeData current = ThemeData.dark();
  void increment() {
    val++;
    notifyListeners();
  }

  void display(String char) {
    userinput = char;
    notifyListeners();
  }
}

Actually when you use context, instead of this use this.context .实际上,当您使用上下文时,而不是使用this.context This will take member variable context not the above line written 'context' under the parameter of creare.这将采用成员变量上下文而不是上面写在 creare 参数下的 'context' 行。

void main(){
  runApp(ChangeNotifierProvider<Data>(
      create: (context) => Data(),
      child: MyApp(),
    ),);
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<Data>(
      builder: (context, appState, child) {
        return MaterialApp(
          theme: Provider.of<Data>(context).current,
          debugShowCheckedModeBanner: false,
          home: Page1(),
        );
      },
    );
  }
}

Wrap your material app with consumer and try this code once.用消费者包装您的材料应用程序并尝试一次此代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 提供者错误:找不到正确的提供者<Color>在此 MyApp 小部件上方 - Provider Error: Could not find the correct Provider<Color> above this MyApp Widget 在此 MyApp 小部件上方找不到正确的提供程序 - Could not find the correct provider above this MyApp widget 错误:找不到正确的提供者<signinprovider>在这个 MyApp 小部件上方</signinprovider> - Error: Could not find the correct Provider<SignInProvider> above this MyApp Widget 颤振提供者错误:在此小部件上方找不到正确的提供者 - 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" 找不到正确的提供者<movies>在这个 MyApp 小部件上方</movies> - Could not find the correct Provider<Movies> above this MyApp Widget Flutter 错误:找不到正确的提供程序<user>在此 Wrapper Widget 上方</user> - Flutter Error: Could not find the correct Provider<User> above this Wrapper 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 - Could not find the correct Provider above this Widget - Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM