-
Notifications
You must be signed in to change notification settings - Fork 104
/
stream_builder.dart
49 lines (45 loc) · 1.42 KB
/
stream_builder.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import 'package:flutter/material.dart';
class MyStreamBuilder extends StatefulWidget {
const MyStreamBuilder({Key? key}) : super(key: key);
@override
State<MyStreamBuilder> createState() => _MyStreamBuilderState();
}
// FAKE FIREBASE STREAM
Stream<int> generateStream = (() async* {
await Future<void>.delayed(const Duration(seconds: 2));
yield 1;
await Future<void>.delayed(const Duration(seconds: 1));
yield 2;
await Future<void>.delayed(const Duration(seconds: 1));
yield 3;
await Future<void>.delayed(const Duration(seconds: 2));
yield 4;
await Future<void>.delayed(const Duration(seconds: 1));
yield 5;
})();
class _MyStreamBuilderState extends State<MyStreamBuilder> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('StreamBuilder')),
body: Center(
child: StreamBuilder(
stream: generateStream,
initialData: 0,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator.adaptive();
} else if (snapshot.hasError) {
return const Text('Error');
} else {
return Text(
snapshot.data.toString(),
style: const TextStyle(fontSize: 40),
);
}
},
),
),
);
}
}