Created
June 30, 2020 18:11
-
-
Save MariaMelnik/ef914cfa0bc4c40b3e7b837f197ec793 to your computer and use it in GitHub Desktop.
Flutter: StreamBuilder inside StreamBuilder (web test).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({Key key}) : super(key: key); | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
Stream<int> outerStream; | |
Stream<int> innerStream; | |
@override | |
void initState() { | |
super.initState(); | |
outerStream = Stream | |
.fromIterable([1, 2]) | |
.asyncMap((int intEvent) => Future.delayed(Duration(seconds: 3), () => intEvent)).asBroadcastStream(); | |
innerStream = Stream | |
.fromIterable([3, 4]) | |
.asyncMap((int intEvent) => Future.delayed(Duration(seconds: 4), () => intEvent)).asBroadcastStream(); | |
innerStream.listen((event) { }); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: StreamBuilder<int>( | |
stream: outerStream, | |
builder: (_, snapshot) { | |
if (!snapshot.hasData) return CircularProgressIndicator(); | |
return Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text("outer: ${snapshot.data.toString()}"), | |
StreamBuilder<int>( | |
stream: innerStream, | |
builder: (_, innerSnapshot) { | |
if (!innerSnapshot.hasData) return CircularProgressIndicator(backgroundColor: Colors.red,); | |
return Text("inner: ${innerSnapshot.data.toString()}"); | |
}, | |
) | |
], | |
); | |
} | |
) | |
), | |
); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment