简体   繁体   English

websocket stream debounceTime 在 flutter 中不工作

[英]websocket stream debounceTime not working in flutter

Hi I need to add some delay on my IOWebSocketChannel streem to slow it down.您好,我需要在我的IOWebSocketChannel 流上添加一些延迟以减慢速度。 I used stream.debounceTime(Duration(seconds: 4)) to call the builder method every 4 seconds, but the builder method never runs.我使用 stream.debounceTime(Duration(seconds: 4)) 每 4 秒调用一次构建器方法,但构建器方法从未运行。 If I remove debounceTime, StreamBuilder works properly.如果我删除 debounceTime,StreamBuilder 将正常工作。 Please help me what is wrong with debounceTime and where it should be?请帮我 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);
            },
          )
        ],
      ),
    );
  }
}

I used throttleTime instead of debounceTime我使用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);
            },
          )
        ],
      ),
    );
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM