Prefer Void Public Cubit Methods
newdartrecommended
Prefer void public methods on Cubit instances.
Rationale
Section titled “Rationale”Public methods on Cubit instances should be used to notify the Cubit and
initiate state changes via the emit method. If the caller needs access to any
state information, they should access it from the state instead.
Examples
Section titled “Examples”Avoid non-void public methods on Cubit instances.
BAD:
import 'package:bloc/bloc.dart';
class CounterCubit extends Cubit<int> { CounterCubit() : super(0);
int increment() { emit(state + 1); return state; }}GOOD:
import 'package:bloc/bloc.dart';
class CounterCubit extends Cubit<int> { CounterCubit() : super(0);
void increment() => emit(state + 1);}Enable
Section titled “Enable”To enable the prefer_void_public_cubit_methods rule, add it to your
analysis_options.yaml under bloc > rules:
bloc: rules: - prefer_void_public_cubit_methodsbloc: rules: prefer_void_public_cubit_methods: true