skip to Main Content

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'.

4

Answers


  1. Chosen as BEST ANSWER

    I just repeated the same process again and everything works, I guess the copy paste I did the first time had an error I don't know.


  2. have you tried

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

    or maybe the paths got messed up.

    Login or Signup to reply.
  3. 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;
    
    Login or Signup to reply.
  4. context.read is from Provider package

    For riverpod, you need to use ref.read

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search