Home > Mobile >  Riverpod for flutter doesn't work after copy of project
Riverpod for flutter doesn't work after copy of project

Time:01-24

I had a Flutter project with an app already being developed on local, and copied the files to a new folder so I use it as a repository for Git. After doing the "pubspec get packages" everything is all right except from some lines that are related to Riverpod. If I return to the old folder the exact same code work perfectly finde so I guess I have to do something more. Here there is an example:

final nombreJuegoTextFieldProvider = StateProvider<String>((ref) {
  return '';
});
void updateNombreJuegoTextField(BuildContext context, String nombre) {
  context.read(nombreJuegoTextFieldProvider).state = nombre;
}

And the problem showed is:

The method 'read' isn't defined for the type 'BuildContext'.
Try correcting the name to the name of an existing method, or defining a method named 'read'.

CodePudding user response:

context.read is from Provider package

For riverpod, you need to use ref.read

CodePudding user response:

have you tried

-> deleting .gitignore files -> Run flutter clean project

or maybe the paths got messed up.

CodePudding user response:

You probably haven't copied all the files to the new folder. Repeat copying. This should include all directories and files from the previous version.

Then run two commands:

flutter clean
flutter pub get

In this example of the code presented, the provider's state can be accessed using ref:

ref.read(nombreJuegoTextFieldProvider).state = nombre;
  • Related