Flutter : A member named 'read' is defined in extensions 'ReadContext' and 'BuildContextX' and neither is more spesific

Viewed 1041

I have a button in my application that return :

onPressed: () {
                    return context
                        .read(FavoriteIds.provider.notifier)
                        .toggle(doa.id.toString());
                  },

In this case, i used a riverpod provider. But when i want to import a flutter_bloc package, the read keyword will be error with this message A member named 'read' is defined in extensions 'ReadContext' and 'BuildContextX' and neither is more specific. Try using an extension override to specify the extension you want to to be chosen.

Please help me solve this problem. thank you :)

2 Answers

Here the problem is read() is defined in both ReadContext & BuildContextX extensions. So the compiler is not getting which extension to use.

To solve the error, use : ReadContext(context).read if you wanna access bloc or BuildContextX(context).read() as per your need.

This means you are importing 2 extensions that both supply the same method read. Consider this example:

extension Ext1 on String {
  void foo() => print("from extension 1");
}

extension Ext2 on String {
  void foo() => print("from extension 2");
}

void main() {
  String s = "hello";
  s.foo();
}

What should this code print? There isn't an obvious answer, and to avoid accidental programming errors, Dart prohibits this.

You could try "go-to definition" (ctrl/cmd click in most IDEs) on the read method to navigate to one of the files that it is defined in, and then delete the corresponding import statement.

However, it might be quicker to just delete all the import statements in that file and add them back with autocomplete

Related