-1

Hi I need to add some delay on my IOWebSocketChannel streem to slow it down. I used stream.debounceTime(Duration(seconds: 4)) to call the builder method every 4 seconds, but the builder method never runs. If I remove debounceTime, StreamBuilder works properly. Please help me what is wrong with debounceTime and where it should be?

 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);
            },
          )
        ],
      ),
    );
  }
}
Erfan Eghterafi
  • 4,344
  • 1
  • 33
  • 44

1 Answers1

0

I used throttleTime instead of 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);
            },
          )
        ],
      ),
    );
  }
}
Erfan Eghterafi
  • 4,344
  • 1
  • 33
  • 44