Home > OS >  'Change Notifier Provider' isn't a function error message
'Change Notifier Provider' isn't a function error message

Time:06-20

I am building a spelling app. This is the code that's giving me an error. I don't know what I'm doing wrong.

void main() {
  runApp(ChangeNotifierProvider(create: (_) => Controller(), child: MyApp()));
}

The error message is

'ChangeNotifierProvider' isn't a function.

CodePudding user response:

you should use MultileProvider

like this...

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

and MyApp class uses MultipleProvider.

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<GetProvider>(create: (context) => GetProvider()),
      ],
      builder: (context, child) {
        return const MaterialApp(
          home: HomeScreen(),
        );
      },
    );
  }
}

CodePudding user response:

You can do Like This:

class Person with ChangeNotifier {
  Person({this.name, this.age});

  final String name;
  int age;

  void increaseAge() {
    this.age  ;
    notifyListeners();
  }
}

// here, you can see that the [ChangeNotifierProvider]
// is "wired up" exactly like the vanilla [Provider]
void main() {
  runApp(
    ChangeNotifierProvider(
      create: (_) => Person(name: "Yohan", age: 25),
      child: MyApp(),
    ),
  );
}

CodePudding user response:

void main() {

runApp( MyApp());

}

class MyApp extends StatelessWidget { MyApp({Key? key}) : super(key: key);

@override Widget build(BuildContext context) {

return ChangeNotifierProvider(
  create: (context) => ThemeProvider(),
  builder: (context, _) {
    final provider = Provider.of<ThemeProvider>(context);
    return MaterialApp(
      title: 'Flutter Demo',
      theme: provider.darkMode ? darkMode : lightMode,
    );
  },
);

} }

  • Related