Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite7827c0

Browse files
authored
Create stream-timeout-between-events-in-flutter.dart
1 parent8e04aa7 commite7827c0

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// 🐦 Twitter https://twitter.com/vandadnp
2+
// 🔵 LinkedIn https://linkedin.com/in/vandadnp
3+
// 🎥 YouTube https://youtube.com/c/vandadnp
4+
// 🤝 Want to support my work? https://youtube.com/c/vandadnp/join/
5+
6+
// import 'package:flutter/material.dart';
7+
8+
import'dart:async';
9+
10+
import'package:flutter/material.dart';
11+
import'dart:developer'as devtoolsshow log;
12+
13+
voidmain() {
14+
runApp(
15+
constApp(),
16+
);
17+
}
18+
19+
extensionLogonObject {
20+
voidlog()=> devtools.log(toString());
21+
}
22+
23+
classTimeoutBetweenEvents<E>extendsStreamTransformerBase<E,E> {
24+
finalDuration duration;
25+
26+
constTimeoutBetweenEvents({requiredthis.duration});
27+
28+
@override
29+
Stream<E>bind(Stream<E> stream) {
30+
StreamController<E>? controller;
31+
StreamSubscription? subscription;
32+
Timer? timer;
33+
34+
controller=StreamController(
35+
onListen: () {
36+
subscription= stream.listen(
37+
(data) {
38+
timer?.cancel();
39+
timer=Timer.periodic(duration, (_) {
40+
controller?.addError(
41+
TimeoutBetweenEventsException('Timeout'),
42+
);
43+
});
44+
controller?.add(data);
45+
},
46+
onError: controller?.addError,
47+
onDone: controller?.close,
48+
);
49+
},
50+
onCancel: () {
51+
subscription?.cancel();
52+
timer?.cancel();
53+
},
54+
);
55+
56+
return controller.stream;
57+
}
58+
}
59+
60+
classTimeoutBetweenEventsExceptionimplementsException {
61+
finalString message;
62+
TimeoutBetweenEventsException(this.message);
63+
}
64+
65+
extensionWithTimeoutBetweenEvents<T>onStream<T> {
66+
Stream<T>withTimeoutBetweenEvents(Duration duration)=>
67+
transform(TimeoutBetweenEvents(duration: duration));
68+
}
69+
70+
Stream<String>getNames()async* {
71+
yield'John';
72+
awaitFuture.delayed(constDuration(seconds:1));
73+
yield'Jane';
74+
awaitFuture.delayed(constDuration(seconds:10));
75+
yield'Doe';
76+
}
77+
78+
Future<void>testIt()async {
79+
awaitfor (final nameingetNames().withTimeoutBetweenEvents(
80+
constDuration(
81+
seconds:3,
82+
),
83+
)) {
84+
name.log();
85+
}
86+
}
87+
88+
classAppextendsStatelessWidget {
89+
constApp({
90+
Key? key,
91+
}):super(key: key);
92+
93+
@override
94+
Widgetbuild(BuildContext context) {
95+
returnMaterialApp(
96+
theme:ThemeData.dark(),
97+
debugShowCheckedModeBanner:false,
98+
home:constHomePage(),
99+
);
100+
}
101+
}
102+
103+
classHomePageextendsStatelessWidget {
104+
constHomePage({Key? key}):super(key: key);
105+
106+
@override
107+
Widgetbuild(BuildContext context) {
108+
testIt();
109+
returnScaffold(
110+
appBar:AppBar(
111+
title:constText('Home Page'),
112+
),
113+
);
114+
}
115+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp