Issue
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);
},
)
],
),
);
}
}
Solution
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);
},
)
],
),
);
}
}
Answered By - Erfan Eghterafi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.