avoid-passing-bloc-to-widget

Path
lint/suspicious/avoid-passing-bloc-to-widget
Default
Off, opt in
Origin
Falcon
Domain
bloc
Rule
110 of 512
bloc

Rejects direct Bloc and Cubit arguments to Flutter widget constructors.

Invalid

example.dartdart
import 'support.dart' as app;
import 'package:bloc/bloc.dart' as b;

typedef ViewAlias = app.View;
class ChildView extends app.View {
  ChildView(Object dependency) : super(dependency);
}
class BlocProvider extends app.View {
  BlocProvider.value({required Object value}) : super(value);
}
class BlocBuilder extends app.View {
  BlocBuilder({required Object bloc}) : super(bloc);
}
class BlocListener extends app.View {
  BlocListener({required Object bloc}) : super(bloc);
}
class BlocConsumer extends app.View {
  BlocConsumer({required Object bloc}) : super(bloc);
}
class BlocSelector extends app.View {
  BlocSelector({required Object bloc}) : super(bloc);
}

void dependencies(app.Counter counter, b.Bloc<Object, int> bloc) {
  app.View(counter);
  new app.View(counter);
  app.View.named(dependency: bloc);
  ChildView(counter);
  ViewAlias(counter);
  app.View(app.Counter());
  BlocProvider.value(value: counter);
  BlocBuilder(bloc: counter);
  BlocListener(bloc: counter);
  BlocConsumer(bloc: counter);
  BlocSelector(bloc: counter);
}
Obtain the Bloc or Cubit from the widget context instead of passing it directly.
24void dependencies(app.Counter counter, b.Bloc<Object, int> bloc) {
25 app.View(counter);
Obtain the Bloc or Cubit from the widget context instead of passing it directly.
25 app.View(counter);
26 new app.View(counter);
Obtain the Bloc or Cubit from the widget context instead of passing it directly.
26 new app.View(counter);
27 app.View.named(dependency: bloc);
Obtain the Bloc or Cubit from the widget context instead of passing it directly.
27 app.View.named(dependency: bloc);
28 ChildView(counter);
Obtain the Bloc or Cubit from the widget context instead of passing it directly.
28 ChildView(counter);
29 ViewAlias(counter);
Obtain the Bloc or Cubit from the widget context instead of passing it directly.
29 ViewAlias(counter);
30 app.View(app.Counter());
Obtain the Bloc or Cubit from the widget context instead of passing it directly.
30 app.View(app.Counter());
31 BlocProvider.value(value: counter);
Obtain the Bloc or Cubit from the widget context instead of passing it directly.
31 BlocProvider.value(value: counter);
32 BlocBuilder(bloc: counter);
Obtain the Bloc or Cubit from the widget context instead of passing it directly.
32 BlocBuilder(bloc: counter);
33 BlocListener(bloc: counter);
Obtain the Bloc or Cubit from the widget context instead of passing it directly.
33 BlocListener(bloc: counter);
34 BlocConsumer(bloc: counter);
Obtain the Bloc or Cubit from the widget context instead of passing it directly.
34 BlocConsumer(bloc: counter);
35 BlocSelector(bloc: counter);

Valid

example.dartdart
import 'support.dart' as real;
import 'package:flutter_bloc/flutter_bloc.dart' as framework;
import 'package:flutter/widgets.dart' as flutter;

class Widget {
  Widget(Object dependency);
}
class LocalView extends Widget {
  LocalView(Object dependency) : super(dependency);
}
class Bloc<E, S> {}
class Cubit<S> {}

class LookupView extends real.View {
  LookupView() : super(0);
  static LookupView of(real.Counter counter) => LookupView();
}

void boundaries(real.Counter counter, dynamic unknown) {
  Widget(counter);
  LocalView(counter);
  real.Repository(counter);
  real.View(Bloc<Object, int>());
  real.View(Cubit<int>());
  real.View(real.Repository(0));
  real.View(unknown);
  LookupView.of(counter);
  framework.BlocProvider.value(value: counter, child: flutter.SizedBox());
  framework.RepositoryProvider.value(value: counter, child: flutter.SizedBox());
  framework.BlocBuilder<real.Counter, int>(
    bloc: counter,
    builder: (context, state) => flutter.SizedBox(),
  );
  framework.BlocListener<real.Counter, int>(
    bloc: counter,
    listener: (context, state) {},
    child: flutter.SizedBox(),
  );
  framework.BlocConsumer<real.Counter, int>(
    bloc: counter,
    builder: (context, state) => flutter.SizedBox(),
    listener: (context, state) {},
  );
  framework.BlocSelector<real.Counter, int, bool>(
    bloc: counter,
    selector: (state) => state > 0,
    builder: (context, selected) => flutter.SizedBox(),
  );
  final View = (Object dependency) => real.Repository(dependency);
  View(counter);
}

Configure avoid-passing-bloc-to-widget

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "avoid-passing-bloc-to-widget": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/suspicious/avoid-passing-bloc-to-widget: <reason>