Avoid Public Bloc Methods
این محتوا هنوز به زبان شما در دسترس نیست.
newdartrecommended
Avoid exposing public methods on Bloc
instances.
Rationale
Section titled “Rationale”Blocs react to incoming events and emit outgoing states. As a result, the
recommended way of communicating with a bloc instance is via the add
method.
In most cases, there’s no need to create additional abstractions on top of the
add
API.
Examples
Section titled “Examples”DO NOT expose public methods on bloc instances.
BAD:
import 'package:bloc/bloc.dart';
enum CounterEvent { increment };
class CounterBloc extends Bloc<CounterEvent, int> { CounterBloc() : super(0) { on<CounterEvent>((event, emit) => emit(state + 1)); }
// Avoid public bloc methods! // Prefer to use [add] directly. void increment() => add(CounterEvent.increment);}
GOOD:
import 'package:bloc/bloc.dart';
enum CounterEvent { increment };
class CounterBloc extends Bloc<CounterEvent, int> { CounterBloc() : super(0) { on<CounterEvent>((event, emit) => emit(state + 1)); }}
Enable
Section titled “Enable”To enable the avoid_public_bloc_methods
rule, add it to your
analysis_options.yaml
under bloc
> rules
:
bloc: rules: - avoid_public_bloc_methods
bloc: rules: avoid_public_bloc_methods: true