1

我的 ApplicationBloc 是小部件树的根。在 bloc 的构造函数中,我正在收听来自存储库的流,其中包含从 JSON 解码的模型,并将它们转发到 StreamBuilder 收听的另一个流。

我希望 StreamBuilder 会一个一个接收模型并将它们添加到 AnimatedList 中。但是有一个问题:StreamBuilder 的构建器只对流中的最后一项触发一次。

例如,有几个模型位于本地存储中,id 为 0、1、2 和 3。所有这些都是从存储库发出的,所有这些都成功放入流控制器,但只有最后一个模型(id == 3 ) 出现在 AnimatedList 中。

存储库:

class Repository {
  static Stream<Model> load() async* {
    //...
    for (var model in models) {
      yield Model.fromJson(model);
    }
  }
}

集团:

class ApplicationBloc {
  ReplaySubject<Model> _outModelsController = ReplaySubject<Model>();
  Stream<Model> get outModels => _outModelsController.stream;

  ApplicationBloc() {
    TimersRepository.load().listen((model) => _outModelsController.add(model));
  }
}

主要飞镖:

void main() {
  runApp(
    BlocProvider<ApplicationBloc>(
      bloc: ApplicationBloc(),
      child: MyApp(),
    ),
  );
}

//...

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    final ApplicationBloc appBloc = //...

    return MaterialApp(
      //...
      body: StreamBuilder(
        stream: appBloc.outModels,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            var model = snapshot.data;
            /* inserting model to the AnimatedList */
          }

          return AnimatedList(/* ... */);
        },
      ),
    );
  }
}

有趣的通知:在 StreamBuilder 的 _subscribe() 方法中,onData() 回调触发了所需的次数,但 build() 方法只触发了一次。

4

1 回答 1

2

您需要 aStream输出 aList<Model而不是单个元素。此外,侦听一个流以将其添加到另一个ReplaySubject流将使输出流延迟 2 (!!!) 帧,因此最好使用单个链。

class TimersRepository {
  // maybe use a Future if you only perform a single http request!
  static Stream<List<Model>> load() async* {
    //...
    yield models.map((json) => Model.fromJson(json)).toList();
  }
}

class ApplicationBloc {
  Stream<List<Model>> get outModels => _outModels;
  ValueConnectableObservable<List<Model>> _outModels;
  StreamSubscription _outModelsSubscription;

  ApplicationBloc() {
    // publishValue is similar to a BehaviorSubject, it always provides the latest value,
    // but without the extra delay of listening and adding to another subject
    _outModels = Observable(TimersRepository.load()).publishValue();

    // do no reload until the BLoC is disposed
    _outModelsSubscription = _outModels.connect();
  }

  void dispose() {
    // unsubcribe repo stream on dispose
    _outModelsSubscription.cancel();
  }
}

class _MyAppState extends State<MyApp> {
  ApplicationBloc _bloc;

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<List<Model>>(
      stream: _bloc.outModels,
      builder: (context, snapshot) {
        final models = snapshot.data ?? <Model>[];
        return ListView.builder(
          itemCount: models.length,
          itemBuilder: (context, index) => Item(model: models[index]),
        );
      },
    );
  }
}
于 2018-12-23T13:19:40.157 回答