Why is the class ChangeNotifierProvider not defined?

Viewed 7460

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>()
}
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.

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

Related