'_StreamHandlerTransformer <dynamic,dynamic>'类型不是'StreamTransformer <Set <void>,dynamic>'类型的子类型

时间:2019-06-20 13:22:49

标签: flutter dart

尝试从StreamTransform获取数据时遇到一些问题

我无法理解此处的正确数据类型

未捕获的异常: TypeError:'_StreamHandlerTransformer'的实例:类型'_StreamHandlerTransformer'不是'StreamTransformer类型的子类型,动态>

import 'dart:async';
class Cake{}

class Order{
  String type;
  Order(this.type);
}

void main(){
  final controller = new StreamController();

  final order = new Order('vainilla');

 final baker = StreamTransformer.fromHandlers(
       handleData: (cakeType, sink){
           print(cakeType); 
           if(cakeType == 'chocolate'){
              sink.add(new Cake());
           }else{
             sink.addError("No puedo hacer este pastel");
           }
       } 
 );   

  controller.sink.add(order);
  controller.stream
    .map((resp) =>  { print(resp.type) } ) //maybe the sour of  the problem type is here 
    .transform(baker)
    .listen( (cake) => print('aquí esta su pastel $cake'),
              onError: (err) => print(err) 
           );
}


3 个答案:

答案 0 :(得分:0)

您需要在handleData回调中指定类型。

final baker = StreamTransformer.fromHandlers(
      handleData: (Set cakeType, sink){
        print(cakeType);
        if(cakeType == 'chocolate'){
          sink.add(new Cake());
        }else{
          sink.addError("No puedo hacer este pastel");
        }
      }
  );

答案 1 :(得分:0)

这是我建立的解决方案

final baker = StreamTransformer<Set<void>, dynamic>.fromHandlers(handleData: (cakeType, sink) {

    if (cakeType == 'chocolate') {
      sink.add(new Cake());
    } else {
      sink.addError("No puedo hacer este pastel");
    }
  });

答案 2 :(得分:0)

我遇到了同样的问题。但是当我在 .map() 内的回调函数中删除大括号时它起作用了 所以输出将是:

import 'dart:async';
class Cake{}

class Order{
  String type;
  Order(this.type);
}

void main(){
  final controller = new StreamController();

  final order = new Order('vainilla');

 final baker = StreamTransformer.fromHandlers(
       handleData: (cakeType, sink){
           if(cakeType == 'chocolate'){
              sink.add(new Cake());
           }else{
             sink.addError("No puedo hacer este pastel");
           }
       } 
 );   

  controller.sink.add(order);
  controller.stream
    .map((resp) =>  resp.type) 
    .transform(baker)
    .listen((cake) => print('aquí esta su pastel $cake'),
              onError: (err) => print(err) 
           );
}