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>()
}
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With