Flutter 无法通过ScanStreamTransformer正确检索模型

Flutter 无法通过ScanStreamTransformer正确检索模型,flutter,dart,Flutter,Dart,我正在尝试使用在flifter中实现BLoC逻辑的流从我的存储库中检索ItemModel类 问题是找到了ItemModel,但没有检索到它。因此,会触发另一个循环并尝试返回一个不同的对象,该对象不会在小部件上检索 样本输出: I/flutter ( 4520): trying to get 25314126 from cache I/flutter ( 4520): 25314126 in transformer I/flutter ( 4520): 25314126 found !! retu

我正在尝试使用在flifter中实现
BLoC
逻辑的流从我的存储库中检索
ItemModel

问题是找到了
ItemModel
,但没有检索到它。因此,会触发另一个循环并尝试返回一个不同的对象,该对象不会在小部件上检索

样本输出:

I/flutter ( 4520): trying to get 25314126 from cache
I/flutter ( 4520): 25314126 in transformer
I/flutter ( 4520): 25314126 found !! returning: {id: 25314126, type: story, by: xucheng, time: 1607172267, text: , parent: null, kids: [25314805,25314781,25314684,25314664], dead: 0, deleted: 0, url: https://bitbashing.io/std-visit.html, score: 24, title: std::visit is everything wrong with modern C++ (2017), descendants: 4}
I/flutter ( 4520): ----------item to find: 25314126 and found: Instance of 'ItemModel'
I/flutter ( 4520): returned item Instance of 'ItemModel'
此处找到并检索了正确的对象,但未在UI上接收到,因此立即进行另一次搜索:

I/flutter ( 4520): trying to get 25314805 from cache
I/flutter ( 4520): 25314805 found !! returning: {id: 25314805, type: comment, by: vasama, time: 1607178963, text: We got here because adding library features is a lot easier and less risky than adding language features. Work on pattern matching [1] is ongoing, but the bar for entry for a language feature is much higher and as such takes a longer time.<p>1. <a href="http:&#x2F;&#x2F;wg21.link&#x2F;p1371" rel="nofollow">http:&#x2F;&#x2F;wg21.link&#x2F;p1371</a>, parent: 25314126, kids: [], dead: 0, deleted: 0, url: null, score: null, title: null, descendants: 0}
I/flutter ( 4520): ----------item to find: 25314805 and found: Instance of 'ItemModel'
I/flutter ( 4520): returned item Instance of 'ItemModel'
这表明
流:bloc.itemWithComments,

这是我的团队课程:

class CommentsBloc{
  final _repository = Repository();
  final _commentsFetcher = PublishSubject<int>();
  final _commentsOutput = BehaviorSubject<Map<int,Future<ItemModel>>>();

  //Streams
  get itemWithComments =>  _commentsOutput.stream;

  //Sink
  Function(int) get fetchItemWithComments => _commentsFetcher.sink.add;

  CommentsBloc(){
    _commentsFetcher.stream.transform(_commentsTransformer()).pipe(_commentsOutput);
  }

  _commentsTransformer (){
    return ScanStreamTransformer(
        (cache,int id,index){
          cache[id] = _repository.fetchItem(id);
          print('$id in transformer');
          cache[id].then((ItemModel item){
            item.kids.forEach((kidId)=>fetchItemWithComments(kidId));

          });
        },
      <int,Future<ItemModel>>{},
    );
  }


  dispose(){
    _commentsFetcher.close();
    _commentsOutput.close();
  }

}
即使每次都返回一个实例,为什么
snapshot.hasData
为false


还请注意,通过使用
onTap
方法,该小部件仅被调用一次,id正确。我完全错过了什么?

即使我使用了
fetchItemWithComments
来接收项目,我仍然必须在
ScanStreamTransformer

\u注释格式(){
回程扫描变压器(
(Mapcache,int id,索引){
打印(索引);
cache[id]=\u repository.fetchItem(id);
缓存[id]。然后((ItemModel项){
item.kids.forEach((kidId)=>fetchItemWithComments(kidId));
});
return cache;//缺少此
},
{},
);
}
这已经困扰了我好几天了,我检查了所有东西,但找不到任何东西,所以如果您使用的是
ScanStreamTransformer

 List<Source> sources = <Source>[
    newsDbProvider,
    NewsApiProvider(),
  ];
  List<Cache> caches = <Cache>[
   newsDbProvider,
  ];

Future<ItemModel> fetchItem(int id) async{

    ItemModel item;
    var source;

    for(source in sources) {
        item = await source.fetchItem(id);
        print('----------item to find: $id and found: ${item}');
        if(item!=null){
         break;
        }
      }

    for(var cache in caches) {
      if(cache!=source) {
        cache.addItem(item);
      }
    }

    print('returned item ${item}');

    return item;

  }
始终有一个
return
语句

 List<Source> sources = <Source>[
    newsDbProvider,
    NewsApiProvider(),
  ];
  List<Cache> caches = <Cache>[
   newsDbProvider,
  ];

Future<ItemModel> fetchItem(int id) async{

    ItemModel item;
    var source;

    for(source in sources) {
        item = await source.fetchItem(id);
        print('----------item to find: $id and found: ${item}');
        if(item!=null){
         break;
        }
      }

    for(var cache in caches) {
      if(cache!=source) {
        cache.addItem(item);
      }
    }

    print('returned item ${item}');

    return item;

  }