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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| import 'package:flutter/material.dart';
Future<String> mockNetwork() async { return Future.delayed( Duration(seconds: 3), () => "Data from network, u know...", ); }
Stream<int> counter() { return Stream.periodic(Duration(seconds: 1), (i) { return i; }); }
class StreamBuilderPage2 extends StatelessWidget { const StreamBuilderPage2({Key? key}) : super(key: key);
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text("Stream Builder test 2"), ), body: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ const SizedBox( height: 20, ), Center( child: StreamBuilder<int>( stream: counter(), builder: (BuildContext context, AsyncSnapshot<int> snapshot) { if (snapshot.hasError) { return Text("Error: ${snapshot.error}"); } switch (snapshot.connectionState) { case ConnectionState.none: return Text("No stream"); case ConnectionState.waiting: return Text("Wating"); case ConnectionState.active: return Text("active: ${snapshot.data}"); case ConnectionState.done: return Text("Stream closed..."); } }, ), ), const SizedBox( height: 20, ), FutureBuilder( builder: (BuildContext context, AsyncSnapshot<String> snapshot) { if (snapshot.connectionState == ConnectionState.done) { if (snapshot.hasError) { return Text("Error: ${snapshot.error}"); } else { return Text("Success: ${snapshot.data}"); } } else { return CircularProgressIndicator(); } }, future: mockNetwork(), ) ], ), ); } }
|