简体   繁体   English

如何使用 StreamTransformer 从数据库 Stream 创建 StreamController

[英]How to create a StreamController from a database Stream using StreamTransformer

I am trying to transform a stream from a database with the database data model to the domain data model.我正在尝试将 stream 从数据库数据 model 转换为域数据 model。

I am quite confused bringing the different pieces of information together.我很困惑将不同的信息放在一起。 While in the StreamTransformer examples that I have found, the stream is always a single object an not a list, my result from the example streaming ObjectBox data, returns Stream<List<PropertyObjectBox>>在我发现的StreamTransformer示例中,stream 始终是单个 object 而不是列表,我从示例流 ObjectBox 数据中得到的结果返回Stream<List<PropertyObjectBox>>

And then it appears as if a piece of the puzzle is missing, how to come from Stream to StreamController .然后看起来好像缺少一块拼图,如何从StreamStreamController

So how what do I have to change at the following code?那么我必须如何更改以下代码?

/// <PropertyObjectBox> data model of Property in ObjectBox database
/// <PropertyModel> data model in data layer (not really needed, I know)
/// <Property> data model in domain layer


@override
// -->> should this return Stream<List<Property>?> or Stream<Property>?
// -->> or something else to comply with handleError?
Stream<List<Property>?> streamOnDeviceProperties() {
  Stream<List<PropertyObjectBox>> propObStream = objectbox.propertyBox.query()
      .watch(triggerImmediately: true).map((query) => query
  // Watching the query produces a Stream<Query<Property>>
  // To get the actual data inside a List<Property>, we need to call find() on the query
      .find());

//-->> again, PropertyObjectBox or List<PropertyObjectBox>?
  var streamTransformer = StreamTransformer<PropertyObjectBox, dynamic>.fromHandlers(
    handleData: (PropertyObjectBox data, EventSink sink) {
      final propertyModel = PropertyModel.fromObjectbox(data);
      final Property property = propertyModel.toDomain();
      sink.add(property);
    },
    handleError: (Object error, StackTrace stacktrace, EventSink sink) {
      sink.addError('Something went wrong: $error');
    },
    handleDone: (EventSink sink) => sink.close(),
  );
//-->> next line causes error 'the getter 'stream' isn't defined for the type 'Stream<List<PropertyObjectBox>>'  
  var controllerStream = propObStream.stream.transform(streamTransformer);

This should do the trick: Updated这应该可以解决问题:已更新

@override
Stream<List<Property>?> streamOnDeviceProperties() {

  Stream<List<PropertyObjectBox>?> propObStream = objectbox.propertyBox.query()
      .watch(triggerImmediately: true).map((query) => query.find());

// List<PropertyObjectBox>?
  StreamTransformer<List<PropertyObjectBox>?,List<Property>?> streamTransformer = StreamTransformer<List<PropertyObjectBox>?,List<Property>?>.fromHandlers(
    handleData: (List<PropertyObjectBox>? data, EventSink sink) {
      var newList = data!.map((value) {
        final propertyModel = PropertyModel.fromObjectbox(value);
        final Property property = propertyModel.toDomain();
        return property;
      }).toList();
      sink.add(newList);
    },
    handleError: (Object error, StackTrace stacktrace, EventSink sink) {
      sink.addError('Something went wrong: $error');
    },
    handleDone: (EventSink sink) => sink.close(),
  );
  
 // if you need a Controller although I don't know why
  Stream<List<Property>?> newStream = propObStream.transform(streamTransformer);
  
  final StreamController<List<Property>?> streamController = StreamController<List<Property>?>(
    onPause: () => print('Paused'),
    onResume: () => print('Resumed'),
    onCancel: () => print('Cancelled'),
    onListen: () => print('Listens'),
  );
  streamController.addStream(newStream);
//********************************
  
  return streamTransformer.bind(propObStream);
}

Access the stream like this:像这样访问 stream:

Stream<List<Property>?> _mystream = streamOnDeviceProperties();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从多个小部件访问 StreamController Stream - Access StreamController Stream from multiple widgets 如何比较stream,streamController dart中的每个事件 - How to compare each events in stream, streamController dart 如何从 StreamController 获取值? - How to get value from StreamController? StreamController 和 BehaviorSubject 是 Dart 中的流吗? - Is StreamController and BehaviorSubject a Stream in Dart? StreamBuilder 使用 Firestore 快照中的 StreamController - StreamBuilder using StreamController from Firestore snapshots 如何从 Flutter Provider 中的 StreamController 中删除数据? - How to delete data from StreamController in Flutter Provider? 如何创建具有不同输入数据类型的 StreamController & Output - How to create a StreamController that has different Data Types for the Input & Output 固定错误:如何解决返回类型'StreamController<connectivitystatus> ' 不是匿名闭包错误所定义的 'Stream'</connectivitystatus> - Fixed-error: How to Solve The return type 'StreamController<ConnectivityStatus>' isn't a 'Stream', as defined by anonymous closure error 流侦听器和流控制器异步“交谈”未按预期工作 - Stream listener and streamController "talking" asynchronously not working as expected 如何在 flutter 中向 StreamController 添加项目 - How to add items to a StreamController in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM