Nest Stream Builder
Complete Code For Nest Stream Builder In Flutter
main.dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Stream<int> timedCounter(Duration interval, [int maxCount]) async* {
int i = 0;
while (true) {
await Future.delayed(interval);
yield i++;
if (i == maxCount) break;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
backgroundColor: Colors.pink[800],
title: Text("Nest Stream Builder"
)),
body: Center(
child: StreamBuilder<int>(
stream: timedCounter(Duration(seconds: 2), 40),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text("No data");
}
int firstCount = snapshot.data;
return StreamBuilder(
stream: timedCounter(Duration(seconds: 1), 45),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text("No data");
}
int secondCount = snapshot.data;
return Text(
"First Count: $firstCount, Second Count: $secondCount",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.lightGreen
));
});
},
),
),
);
}
}