Flutter中BLoC的原理剖析

平安喜乐

前置知识

想要弄懂BLoC的原理,需要先了解下Stream的相关知识。

StreamController、StreamBuilder这两者的搭配可以轻松实现widget的刷新,来看下使用:

定义view层,初始化用以处理页面数据刷新逻辑的StreamLogic实例,通过StreamBuilder并指定其初始数据、所需要的stream等参数,实现局部widget的刷新;
借用StatefulWidget中的dispose()方法,释放掉StreamLogic实例;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:state_management_demo/stream_demo/stream_logic.dart';
import 'package:state_management_demo/stream_demo/stream_state.dart';

class StreamPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => StreamPageState();
}

class StreamPageState extends State<StreamPage> {
  final logic = StreamLogic();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Stream Page'),),
      body: Center(
        child: StreamBuilder<StreamState>(
          initialData: logic.state,
          stream: logic.stream,
          builder: (context, snapshot) {
            return Text(
              'current count: ${snapshot.data!.count}',
              style: TextStyle(color: Colors.black87, fontSize: 16),
            );
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          logic.addCount();
        },
        child: Icon(Icons.add),
      ),
    );
  }

  @override
  void dispose() {
    logic.dispose();
    super.dispose();
  }
}

定义logic层,初始化用于存储数据的StreamState实例,初始化StreamController,通过泛型指定StreamController实例的数据源;

import 'dart:async';

import 'package:state_management_demo/stream_demo/stream_state.dart';

class StreamLogic {
  final state = StreamState();

  final _controller = StreamController<StreamState>.broadcast();
  Stream<StreamState> get stream => _controller.stream;

  addCount() {
    _controller.add(state..count += 1);
  }

  dispose() {
    _controller.close();
  }
}

定义state层

class StreamState {
  int count = 0;
}

通过以上代码,便可以实现一个简易的点击按钮刷新页面数据的功能,不过其中有如下几个问题:

  • 需要手动创建Stream的一系列对象;
  • Stream流必须要有关闭操作,所以需要使用StatefulWidget;
  • 至少需要手动指定StreamBuilder得三个必传参数;

在BLoC中,作者通过Provider中的InheritedProvider控件,解决了以上痛点。

BLoC的刷新机制

BLoC的刷新机制,本质上就是对上述Stream的使用进行了封装,我们可以看看BLoC中几个关键类的实现;

BlocProvider

BlocProvider内部封装了Provider中的InheritedProvoider,用以实现刷新参数的精简与Stream流的关闭,我们来看看BlocProvider的源码:

mixin BlocProviderSingleChildWidget on SingleChildWidget {}

class BlocProvider<T extends BlocBase<Object?>>
    extends SingleChildStatelessWidget with BlocProviderSingleChildWidget {
  const BlocProvider({
    Key? key,
    required Create<T> create,
    this.child,
    this.lazy = true,
  })  : _create = create,
        _value = null,
        super(key: key, child: child);

  const BlocProvider.value({
    Key? key,
    required T value,
    this.child,
  })  : _value = value,
        _create = null,
        lazy = true,
        super(key: key, child: child);

  final Widget? child;

  final bool lazy;

  final Create<T>? _create;

  final T? _value;

  static T of<T extends BlocBase<Object?>>(
    BuildContext context, {
    bool listen = false,
  }) {
    try {
      return Provider.of<T>(context, listen: listen);
    } on ProviderNotFoundException catch (e) {
      if (e.valueType != T) rethrow;
      throw FlutterError(
        '''
        错误提示
        ''',
      );
    }
  }

  @override
  Widget buildWithChild(BuildContext context, Widget? child) {
    assert(
      child != null,
      '$runtimeType used outside of MultiBlocProvider must specify a child',
    );
    final value = _value;
    return value != null
        ? InheritedProvider<T>.value(
            value: value,
            startListening: _startListening,
            lazy: lazy,
            child: child,
          )
        : InheritedProvider<T>(
            create: _create,
            dispose: (_, bloc) => bloc.close(),
            startListening: _startListening,
            child: child,
            lazy: lazy,
          );
  }

  static VoidCallback _startListening(
    InheritedContext<BlocBase?> e,
    BlocBase value,
  ) {
    final subscription = value.stream.listen(
      (dynamic _) => e.markNeedsNotifyDependents(),
    );
    return subscription.cancel;
  }
}

BlocProvider和BlocProvider.value的区别:

  • BlocProvider.value没有对其传入的Bloc做关闭操作,因此BlocProvider.value适用于全局Bloc实例;
  • 单页面Bloc需要使用BlocProvider来创建相应的Bloc/Cubit;

BlocProvider.of方法

  • 作用:可以被BlocProvider包裹的子控件中,通过BlocProvider.of获取BlocProvider.create时传入的XXXBloc实例;
  • 依据InheritedProvoider的特性,只有在被BlocProvider包裹的子控件中可以获取到XXXBloc实例,BlocProvider的父布局无法获取;

create即外部实例化的XXXBloc,BlocProvider将其传入到其内部Provider的InheritedProvoider中

  • _startListening方法其实没有起作用,根据Provider的特性,markNeedsNotifyDependents()方法需要通过与Provider.of(context, listen: true)配合使用才能生效,但在_startListening方法中,没有使用Provider.of(context, listen: true)来注册widget;

总结:

  • BlocProvider会存储外部传入的XXXBloc实例,并存储在InheritedProvoider中;
  • 可以通过BlocProvider.of方法获取BlocProvider中存储的XXXBloc实例(必须是BlocProvider的子控件);
  • 存储在BlocProvider中的XXXBloc实例会自动被释放,存储在BlocProvider.value中的XXXBloc实例不会自动被释放;
BlocProvider内部机制

BlocBase

BlocBase是一个抽象类,为我们自定义的Bloc提供了基础功能;

abstract class BlocBase<State>
    implements StateStreamableSource<State>, Emittable<State>, ErrorSink {
  BlocBase(this._state) {
    _blocObserver.onCreate(this);
  }
 
  final _blocObserver = BlocOverrides.current?.blocObserver ?? Bloc.observer;
 
  late final _stateController = StreamController<State>.broadcast();
 
  State _state;
 
  bool _emitted = false;
 
  @override
  State get state => _state;
 
  @override
  Stream<State> get stream => _stateController.stream;
 
  @override
  bool get isClosed => _stateController.isClosed;
 
  @protected
  @visibleForTesting
  @override
  void emit(State state) {
    try {
      if (isClosed) {
        throw StateError('Cannot emit new states after calling close');
      }
      if (state == _state && _emitted) return;
      onChange(Change<State>(currentState: this.state, nextState: state));
      _state = state;
      _stateController.add(_state);
      _emitted = true;
    } catch (error, stackTrace) {
      onError(error, stackTrace);
      rethrow;
    }
  }
 
  @protected
  @mustCallSuper
  void onChange(Change<State> change) {
    _blocObserver.onChange(this, change);
  }
 
  @protected
  @mustCallSuper
  @override
  void addError(Object error, [StackTrace? stackTrace]) {
    onError(error, stackTrace ?? StackTrace.current);
  }
 
 
  @protected
  @mustCallSuper
  void onError(Object error, StackTrace stackTrace) {
    _blocObserver.onError(this, error, stackTrace);
  }
 
  @mustCallSuper
  @override
  Future<void> close() async {
    await _stateController.close();
  }
}

BlocBase主要做了以下几件事:

  • 存储了外部传入的state实例,每次使用emit()方法刷新时,会使用新的state实例替换旧的state;
  • emit()方法中或做一个判断,如果传入的新state实例与旧的state相同,则不进行刷新操作;
  • 初始化了Stream的一系列对象,封装了关闭Stream流的操作;

我们可以对BlocBase的代码进行一定的精简:

abstract class BlocBase<T> {
  BlocBase(this._state) {}
 
  final _stateController = StreamController<T>.broadcast();
 
  T _state;
 
  @override
  T get state => _state;
 
  @override
  Stream<T> get stream => _stateController.stream;
 
  void emit(T state) {
      if (_stateController.isClosed) return;
      if (state == _state) return;
      _state = state;
      _stateController.add(_state);
  }
 
  @mustCallSuper
  Future<void> close() async {
    await _stateController.close();
  }
}

这样可以比较直观的看出BlocBase内部的逻辑。

BlocBuilder

BlocBuilder是对StreamBuilder进行的一次封装,精简了其使用方式;

typedef BlocWidgetBuilder<S> = Widget Function(BuildContext context, S state);
 
typedef BlocBuilderCondition<S> = bool Function(S previous, S current);
 
class BlocBuilder<B extends BlocBase<S>, S> extends BlocBuilderBase<B, S> {
  const BlocBuilder({
    Key? key,
    required this.builder,
    B? bloc,
    BlocBuilderCondition<S>? buildWhen,
  }) : super(key: key, bloc: bloc, buildWhen: buildWhen);
 
  final BlocWidgetBuilder<S> builder;
 
  @override
  Widget build(BuildContext context, S state) => builder(context, state);
}

BlocBuilder中的buildWhen是判断是否需要更新的参数;
BlocBuilder内部只是调用了传入的builder,因此需要看下BlocBuilder的父类 —— BlocBuilderBase;

abstract class BlocBuilderBase<B extends BlocBase<S>, S>
    extends StatefulWidget {
  const BlocBuilderBase({Key? key, this.bloc, this.buildWhen})
      : super(key: key);
 
  final B? bloc;
 
  final BlocBuilderCondition<S>? buildWhen;
 
  Widget build(BuildContext context, S state);
 
  @override
  State<BlocBuilderBase<B, S>> createState() => _BlocBuilderBaseState<B, S>();
}
 
class _BlocBuilderBaseState<B extends BlocBase<S>, S>
    extends State<BlocBuilderBase<B, S>> {
  late B _bloc;
  late S _state;
 
  @override
  void initState() {
    super.initState();
    _bloc = widget.bloc ?? context.read<B>();
    _state = _bloc.state;
  }
 
  @override
  Widget build(BuildContext context) {
    if (widget.bloc == null) {
      context.select<B, bool>((bloc) => identical(_bloc, bloc));
    }
    return BlocListener<B, S>(
      bloc: _bloc,
      listenWhen: widget.buildWhen,
      listener: (context, state) => setState(() => _state = state),
      child: widget.build(context, _state),
    );
  }
}

BlocBuilderBase本质上是一个StatefulWidget,与其对应的_BlocBuilderBaseState相关联;
在_BlocBuilderBaseState中通过context.read<B>()方法获取我们在BlocProvider中传入的XXXBloc实例对象,context.read<B>()是对Provider.of<T>()的一次封装,效果相同,随后将获取到的XXXBloc实例对象、XXXBloc实例对象中的state实例保存在_BlocBuilderBaseState中;
_BlocBuilderBaseState中抽象了一个build方法,初始化了一个BlocListener实例,传入了_BlocBuilderBaseState内部保存的XXXBloc实例、state实例、buildWhen参数、build参数,在_BlocBuilderBaseState源码中并未发现数据刷新的逻辑,因此还需要继续看BlocListener的代码;

class BlocListener<B extends BlocBase<S>, S> extends BlocListenerBase<B, S>
    with BlocListenerSingleChildWidget {
  const BlocListener({
    Key? key,
    required BlocWidgetListener<S> listener,
    B? bloc,
    BlocListenerCondition<S>? listenWhen,
    Widget? child,
  }) : super(
          key: key,
          child: child,
          listener: listener,
          bloc: bloc,
          listenWhen: listenWhen,
        );
}

BlocListener内部无任何初始化外的操作,需要看下其父类BlocListenerBase;

abstract class BlocListenerBase<B extends BlocBase<S>, S>
    extends SingleChildStatefulWidget {
  const BlocListenerBase({
    Key? key,
    required this.listener,
    this.bloc,
    this.child,
    this.listenWhen,
  }) : super(key: key, child: child);
 
  final Widget? child;
 
  final B? bloc;
 
  final BlocWidgetListener<S> listener;
 
  final BlocListenerCondition<S>? listenWhen;
 
  @override
  SingleChildState<BlocListenerBase<B, S>> createState() =>
      _BlocListenerBaseState<B, S>();
}
 
class _BlocListenerBaseState<B extends BlocBase<S>, S>
    extends SingleChildState<BlocListenerBase<B, S>> {
  StreamSubscription<S>? _subscription;
  late B _bloc;
  late S _previousState;
 
  @override
  void initState() {
    super.initState();
    _bloc = widget.bloc ?? context.read<B>();
    _previousState = _bloc.state;
    _subscribe();
  }
 
  @override
  Widget buildWithChild(BuildContext context, Widget? child) {
    return child;
  }
 
  @override
  void dispose() {
    _unsubscribe();
    super.dispose();
  }
 
  void _subscribe() {
    _subscription = _bloc.stream.listen((state) {
      if (widget.listenWhen?.call(_previousState, state) ?? true) {
        widget.listener(context, state);
      }
      _previousState = state;
    });
  }
 
  void _unsubscribe() {
    _subscription?.cancel();
    _subscription = null;
  }
}

关键代码都在BlocListenerBase里了:

  • BlocListenerBase能内部保存传进来的XXXBloc实例,并通过XXXBloc实例获取state实例;
  • 通过_bloc.stream.listen方法,监听XXXBloc内部stream流的变化,由于stream流监听的是特定的state类型,因此可以直接获取状态改变后的新state实例;
  • widget.listener()是真正实现刷新数据的方法,具体实现由外部传入,在BlocBuilderBase中,传入的是setState()方法;

我们可以对BlocBuilder的实现进行一定的精简,只保留其数据刷新的核心逻辑;

class NewBlocBuilder<B extends BlocBase<S>, S> extends StatefulWidget {
  const NewBlocBuilder({
    Key? key,
    required this.builder,
  }) : super(key: key);
 
  final Function(BuildContext context, S state) builder;
 
  @override
  _NewBlocBuilderState createState() => _NewBlocBuilderState<B, S>();
}
 
class _NewBlocBuilderState<B extends BlocBase<S>, S> extends State<EasyBlocBuilder<B, S>> {
  late B _bloc;
  late S _state;
  StreamSubscription<S>? _listen;
 
  @override
  void initState() {
    _bloc = BlocProvider.of<B>(context);
    _state = _bloc.state;
 
    //数据改变刷新Widget
    _listen = _bloc.stream.listen((event) {
      setState(() {});
    });
    super.initState();
  }
 
  @override
  Widget build(BuildContext context) {
    return widget.builder(context, _state);
  }
 
  @override
  void dispose() {
    _listen?.cancel();
    super.dispose();
  }
}

总结

经过上面的分析,我们大致知道了整个BLoC的运行机制

BlocProvider:

  • 存储外部传入的XXXBloc实例对象;
  • 添加.of<T>方法,可以在BlocProvider及其子节点获取BlocProvider中存储的XXXBloc实例对象;
  • Stream流的回收;

BlocBase:

  • 存储外部传入的state实例对象;
  • 初始化Stream相关的一系列对象;
  • 封装了Stream流回收的实现;

BlocBuilder:

  • 基于StatefulWidget实现;
  • 通过BlocProvider获取其内部存储的XXXBloc实例对象,在通过listener方法监听其内部state实例对象的值的改变;
  • 数据改变后,通过setState()方法来实现BlocBuilder内部包裹的控件的刷新;

造轮子:自定义状态管理框架

框架整体思路参考BLoC,进行一定程度的简化;

logic层(SMLogic):定义基类,处理Stream流的一些列操作;

import 'dart:async';
 
class SMLogic<T> {
  SMLogic(this.state) : _controller = StreamController<T>.broadcast();
 
  final StreamController<T> _controller;
 
  late T state;
 
  Stream<T> get stream => _controller.stream;
 
  emit(T newState) {
    if (_controller.isClosed) return;
    if (state == newState) return;
    state = newState;
    _controller.add(state);
  }
 
  Future<void> close() async {
    await _controller.close();
  }
}

provider层(SMProvider):

  • 这里弃用了Provider提供的InheritiedProvider,使用Flutter标准库提供的InheritiedWidget+InheritiedElement替代实现;
  • 自定义.of<T>方法从父控件中获取对应的Element(SMLogic实例);
  • 调用SMLogic实例关闭Stream的操作;
import 'package:flutter/cupertino.dart';
import 'package:state_management_demo/SMTool/state_management_logic.dart';
 
class SMProvider<T extends SMLogic> extends InheritedWidget {
  SMProvider({
    Key? key,
    Widget? child,
    required this.create,
  }) : super(key: key, child: child ?? Container());
 
  T Function(BuildContext context) create;
 
  @override
  bool updateShouldNotify(covariant InheritedWidget oldWidget) {
    return false;
  }
 
  @override
  InheritedElement createElement() {
    return _SMInheritedElement(this);
  }
 
  static T of<T extends SMLogic>(BuildContext context) {
    var ie = context.getElementForInheritedWidgetOfExactType<SMProvider<T>>()
        as _SMInheritedElement<T>?;
    if (ie == null) {
      throw 'not found';
    }
    return ie.value;
  }
}
 
class _SMInheritedElement<T extends SMLogic> extends InheritedElement {
  _SMInheritedElement(SMProvider<T> widget) : super(widget);
 
  bool _firstBuild = true;
 
  late T _value;
  T get value => _value;
 
  @override
  void performRebuild() {
    if (_firstBuild) {
      _firstBuild = false;
      _value = (widget as SMProvider<T>).create(this);
    }
    super.performRebuild();
  }
 
  @override
  void unmount() {
    _value.close();
    super.unmount();
  }
}

builder层:基于StatefulWidget;

import 'dart:async';
 
import 'package:flutter/cupertino.dart';
import 'package:state_management_demo/SMTool/state_management_logic.dart';
import 'package:state_management_demo/SMTool/state_management_provider.dart';
 
class SMBuilder<L extends SMLogic<S>, S> extends StatefulWidget {
  SMBuilder({
    Key? key,
    required this.builder,
  }) : super(key: key);
 
  Function(BuildContext context, S state) builder;
 
  @override
  State<StatefulWidget> createState() {
    return _SMBuilderState<L, S>();
  }
}
 
class _SMBuilderState<L extends SMLogic<S>, S> extends State<SMBuilder<L, S>> {
  late L _logic;
  late S _state;
 
  StreamSubscription<S>? _subscription;
 
  @override
  void initState() {
    _logic = SMProvider.of<L>(context);
    _state = _logic.state;
    _subscription = _logic.stream.listen((state) {
      setState(() {
 
      });
    });
    super.initState();
  }
 
  @override
  Widget build(BuildContext context) {
    return widget.builder(context, _state);
  }
 
  @override
  void dispose() {
    _subscription?.cancel();
    super.dispose();
  }
}

通过以上方式便可实现BLoC的基础功能;

框架的使用

实现一个点击按钮增加的计数的功能;

state层:


class SMDemoState {
  late int count;

  SMDemoState init() {
    return SMDemoState()..count = 0;
  }

  SMDemoState clone() {
    return SMDemoState()..count = count;
  }
}

logic层:

import 'package:state_management_demo/SMTool/state_management_logic.dart';
import 'package:state_management_demo/sm_demo_state.dart';

class SMDemoLogic extends SMLogic<SMDemoState> {
  SMDemoLogic() : super(SMDemoState().init());

  void addCount() {
    emit(state.clone()..count = ++state.count);
  }
}

view层:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:state_management_demo/SMTool/state_management_builder.dart';
import 'package:state_management_demo/SMTool/state_management_provider.dart';
import 'package:state_management_demo/sm_demo_logic.dart';
import 'package:state_management_demo/sm_demo_state.dart';

class SMDemoPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SMProvider(
      create: (BuildContext context) => SMDemoLogic(),
      child: Builder(
        builder: (context) => _buildPage(context),
      ),
    );
  }

  Widget _buildPage(BuildContext context) {
    final logic = SMProvider.of<SMDemoLogic>(context);
    return Scaffold(
      appBar: AppBar(title: Text('自定义状态管理框架-SMTool范例'),),
      body: Center(
        child: SMBuilder<SMDemoLogic, SMDemoState>(
          builder: (context, state) {
            return Text(
              'You have pushed the button this many times: ${logic.state.count}',
            );
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => logic.addCount(),
        child: Icon(Icons.add),
      ),
    );
  }
}

demo源码地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容