使用流的 BLoC 模式
学习 Flutter 中的 BLoC(业务逻辑组件)模式,使用事件、状态和流将业务逻辑与界面分离。
使用流的 BLoC 模式 是 CoddyKit 上的免费 Flutter Mobile Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flutter Mobile Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flutter Mobile Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Is BLoC?
BLoC stands for Business Logic Component. It is a pattern that keeps your business logic out of widgets by turning events into states through a stream.
- UI sends events in
- BLoC processes them
- New states flow back out to the UI
Why Another Pattern?
You already know setState, Provider, and Riverpod. BLoC shines when logic is complex and you want:
- Clear separation of UI and logic
- Predictable, testable state transitions
- A reactive stream-based flow
Streams Refresher
A Stream is a sequence of asynchronous values over time. A StreamController lets you push values and expose them as a stream.
final controller = StreamController<int>();
controller.stream.listen((value) => print(value));
controller.add(1);
controller.add(2);Events
An event is an immutable class describing something the user did. Define them as plain Dart classes.
abstract class CounterEvent {}
class Increment extends CounterEvent {}
class Decrement extends CounterEvent {}States
A state is an immutable snapshot of what the UI should show. Keep it simple and serializable.
class CounterState {
final int count;
const CounterState(this.count);
}The Bloc Class
Using the flutter_bloc package, a Bloc maps events to states with on<Event> handlers.
class CounterBloc extends Bloc<CounterEvent, CounterState> {
CounterBloc() : super(const CounterState(0)) {
on<Increment>((e, emit) => emit(CounterState(state.count + 1)));
on<Decrement>((e, emit) => emit(CounterState(state.count - 1)));
}
}Providing the Bloc
Wrap part of the tree in BlocProvider so descendants can access the bloc.
BlocProvider(
create: (_) => CounterBloc(),
child: const CounterPage(),
);Reacting with BlocBuilder
BlocBuilder rebuilds widgets whenever a new state is emitted.
BlocBuilder<CounterBloc, CounterState>(
builder: (context, state) => Text('Count: ' + state.count.toString()),
);Dispatching Events
Send events to the bloc with context.read<CounterBloc>().add(...).
FloatingActionButton(
onPressed: () => context.read<CounterBloc>().add(Increment()),
child: const Icon(Icons.add),
);BlocListener for Side Effects
Use BlocListener for one-time side effects like showing a SnackBar or navigating, where you do not want to rebuild UI.
BlocListener<CounterBloc, CounterState>(
listener: (context, state) {
if (state.count == 10) ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Reached 10!')),
);
},
child: child,
);Cubit: A Lighter BLoC
A Cubit drops events and exposes methods directly that call emit. Use it when full event classes feel like overkill.
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
void increment() => emit(state + 1);
}Quick Check
Which widget should you use to trigger a one-time SnackBar without rebuilding UI on every state?
Recap
You learned the BLoC pattern:
- Events in, states out, via streams
- Bloc with on<Event> handlers, or a lighter Cubit
- BlocProvider, BlocBuilder and BlocListener
BLoC gives you clean, testable separation of logic and UI for complex apps.
常见问题解答
「使用流的 BLoC 模式」课时是免费的吗?
是的 — 「使用流的 BLoC 模式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flutter Mobile Development 课程的其余内容,请升级到 CoddyKit PRO。 Flutter Mobile Development 课程共包含 4 节课。
「使用流的 BLoC 模式」这节课中我会学到什么?
学习 Flutter 中的 BLoC(业务逻辑组件)模式,使用事件、状态和流将业务逻辑与界面分离。 你通过在浏览器中直接运行的动手代码来练习 Flutter Mobile Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Flutter Mobile Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Flutter Mobile Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用流的 BLoC 模式」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Flutter Mobile Development 课中编写并运行代码吗?
能。每节 Flutter Mobile Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。