websocket 流 debounceTime 无法正常工作

websocket stream debounceTime not working in flutter

嗨,我需要在我的 IOWebSocketChannel 流上添加一些延迟以减慢速度。 我使用 stream.debounceTime(Duration(seconds: 4)) 每 4 秒调用一次构建器方法,但构建器方法从未运行过。如果我删除 debounceTime,StreamBuilder 将正常工作。 请帮助我 debounceTime 有什么问题以及它应该在哪里?

 class _HomeState extends State<Home> {
  IOWebSocketChannel? channel;

  @override
  void initState() {
    super.initState();
    channel?.sink.close();
    channel = IOWebSocketChannel.connect(
      Uri.parse(ApiEndPoint.socketApi),
    );
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      resizeToAvoidBottomPadding: false,
      appBar: AppBar(
        title: Text('price'),
      ),
      body: Column(
        children: <Widget>[
          StreamBuilder(
            // use debounceTime to run builder method every 4 seconds
            stream: channel!.stream.debounceTime(Duration(seconds: 4)),
            builder: (context, snapshot) {
              print(snapshot);
              return createMyList(snapshot.data);
            },
          )
        ],
      ),
    );
  }
}

我用了 throttleTime 而不是 debounceTime

class _HomeState extends State<Home> {
  IOWebSocketChannel? channel;

  @override
  void initState() {
    super.initState();
    channel?.sink.close();
    channel = IOWebSocketChannel.connect(
      Uri.parse(ApiEndPoint.socketApi),
    );
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      resizeToAvoidBottomPadding: false,
      appBar: AppBar(
        title: Text('price'),
      ),
      body: Column(
        children: <Widget>[
          StreamBuilder(
            // use debounceTime to run builder method every 4 seconds
            stream: channel!.stream.throttleTime (Duration(seconds: 4)),
            builder: (context, snapshot) {
              print(snapshot);
              return createMyList(snapshot.data);
            },
          )
        ],
      ),
    );
  }
}