prefer-bloc-extensions

Group
Style
Path
lint/style/prefer-bloc-extensions
Default
Off, opt in
Origin
Falcon
Domain
bloc
Rule
319 of 512
bloc

Prefer context.read/watch over package:flutter_bloc's provider lookups.

Invalid

example.dartdart
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart' as fb;
void example(Object context) {
  BlocProvider.of<int>(context);
  BlocProvider.of<int>(context, listen: false);
  BlocProvider.of<int>(context, listen: true);
  fb.BlocProvider.of<int>(context);
  RepositoryProvider.of<int>(context);
}
Use `context.read` or `context.watch` instead of a provider's `of` method.
3void example(Object context) {
4 BlocProvider.of<int>(context);
Use `context.read` or `context.watch` instead of a provider's `of` method.
4 BlocProvider.of<int>(context);
5 BlocProvider.of<int>(context, listen: false);
Use `context.read` or `context.watch` instead of a provider's `of` method.
5 BlocProvider.of<int>(context, listen: false);
6 BlocProvider.of<int>(context, listen: true);
Use `context.read` or `context.watch` instead of a provider's `of` method.
6 BlocProvider.of<int>(context, listen: true);
7 fb.BlocProvider.of<int>(context);
Use `context.read` or `context.watch` instead of a provider's `of` method.
7 fb.BlocProvider.of<int>(context);
8 RepositoryProvider.of<int>(context);

Valid

example.dartdart
import 'package:flutter_bloc/flutter_bloc.dart' as fb;
class BlocProvider { static int of(Object context) => 0; }
class Custom { int of(Object context) => 0; }
void example(Object context, Custom service, Custom BlocProvider, bool listen) {
  BlocProvider.of(context);
  service.of(context);
  context.read<int>();
  context.watch<int>();
  fb.BlocProvider.of<int>(context, listen: listen);
}
extension LocalContext on Object {
  T read<T>() => throw StateError('fixture');
  T watch<T>() => throw StateError('fixture');
}

Configure prefer-bloc-extensions

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-bloc-extensions": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/prefer-bloc-extensions: <reason>