块模式中的 ScanStreamTransformer

ScanStreamTransformer in bloc pattern

我遇到过这个问题:

正在执行热重启... 正在将文件同步到设备 Android 为 x86 构建的 SDK... lib/blocs/movie_detail_bloc.dart:28:22: 错误:一个比较表达式不能是另一个比较表达式的操作数。 尝试在其中一项比较中加上括号。 未来{}, ^ lib/blocs/movie_detail_bloc.dart:28:13:错误:未为 class 'Type'.

定义运算符“<”

这是我的代码部分=>

class MovieDetailBloc {
  final _repository = Repository();
  final _movieId = PublishSubject<int>();
  final _trailers = BehaviorSubject<Future<Trailers>>();

  Function(int) get fetchTrailersById => _movieId.sink.add;

  Stream<Future<Trailers>> get movieTrailers => _trailers.stream;

  MovieDetailBloc() {
    _movieId.stream.transform(_itemTransformer()).pipe(_trailers);
  }

  _itemTransformer() {
    return ScanStreamTransformer<int, Future<Trailers>>((trailer, int id, index) {
        debugPrint(index.toString());
        trailer = _repository.fetchTrailers(id);
        debugPrint(trailer.toString());
        return trailer;
      },
      Future<Trailers>{},     <---error line--
    );
  }

这是答案,这是有效的。

class MovieDetailBloc {
  final _repository = Repository();
  final _movieId = PublishSubject<int>();
  final _trailers = BehaviorSubject<Future<Trailers>>();
  int id= 0;        //for emptyFuture
  List<Trailer?> trailer = [];  //for emptyFuture

  Function(int) get fetchTrailersById => _movieId.sink.add;

  Stream<Future<Trailers>> get movieTrailers => _trailers.stream;

  MovieDetailBloc() {
    _movieId.stream.transform(_itemTransformer()).pipe(_trailers);
  }

  _itemTransformer() {
    return ScanStreamTransformer<int, Future<Trailers>>((trailer, int id, index) {
        trailer = _repository.fetchTrailers(id);
        return trailer;
      },
      emptyFuture(), //making an unimportant function for null safety
    );
  }

  Future<Trailers> emptyFuture() async {
    return Trailers(id, trailer);
  }