Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the class ChangeNotifierProvider not defined?

Tags:

yaml

flutter

I am trying to use the ChangeNotifierProvider class from Flutter Provider package. However, it is given me an error saying

The method isn't defined for the class MyCustomWidget

I have added provider dependency to my pubspec.yaml file.

I have a custom widget class like this

@override
Widget build(BuildContext context) {
    return ChangeNotifierProvider<Name>()
}
like image 551
Xh Lin Avatar asked Jun 17 '19 08:06

Xh Lin


People also ask

What is create in ChangeNotifierProvider?

Creating a ChangeNotifier:To create a value, use the default constructor. Creating the instance inside build using ChangeNotifierProvider. value will lead to memory leaks and potentially undesired side-effects.

How do I use ChangeNotifierProxyProvider?

ChangeNotifierProxyProvider<MyModel, MyChangeNotifier>( create: (_) => MyChangeNotifier(), update: (_, myModel, myNotifier) => myNotifier .. update(myModel), child: ... ); In that situation, if MyModel were to update, then MyChangeNotifier will be able to update accordingly.


2 Answers

Add the line in your pubspec.yml

dependencies:
flutter:
   sdk: flutter
intl: any
provider: ^5.0.0    # line to be added,don't put after sdk,it should be in 
                      flutter order

and then run flutter pub get after deleting pubspec.lock file and

 import 'package:provider/provider.dart'; 

where ever is required.

like image 140
ashishSober Avatar answered Oct 21 '22 00:10

ashishSober


I had to manually import the provider package (copy and paste) like the following package:provider/provider.dart;

like image 41
Xh Lin Avatar answered Oct 21 '22 01:10

Xh Lin