官方文档
链接: bloc
添加依赖
将bloc
的包作为依赖项(dependencies) 添加到我们的pubspec.yaml
中
在bloc依赖库中选取合适的版本依赖 flutter_bloc: ^6.1.2
dependencies:
flutter:
sdk: flutter
http: ^0.12.2
json_annotation: ^3.1.1
# 此处为添加的依赖
flutter_bloc: ^6.1.2
dev_dependencies:
flutter_test:
sdk: flutter
json_serializable: ^3.5.1
build_runner: ^1.8.0
如果是 Flutter 的话,在命令行中运行 flutter packages get
在 dart 文件中引入该依赖
import 'package:bloc/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
初始项目代码
构建Bloc
查看 bloc 源码
abstract class Bloc<Event, State> extends Cubit<State>
implements EventSink<Event> {
/// {@macro bloc}
Bloc(State initialState) : super(initialState) {
_bindEventsToStates();
}
..