context.select isn't defined for BuildContext

Issue

Why I can’t use any extension methods of Provider (context.select, context.read and context.listen)?

I get a static error like this.

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

class MyWidget extends StatelessWidget {
  

  @override
  Widget build(BuildContext context) {

    // Following line causes the problem
    var isFavorite = context.select<FavModel, bool>(
      (fav) => fav.items.contains(item),
    );

    return OtherWidget(...);
  }
}

Solution

context.select, context.read and context.listen are extension methods from Provider. To use them you should import Provider.

Add this on top of your file:

import 'package:provider/provider.dart';

It should be auto-imported by default. It’s a known issue.

Answered By – easeccy

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published