Last active
January 4, 2023 14:36
-
-
Save kumar-aakash86/a7986c44180ef0cb6555405ec25b482d to your computer and use it in GitHub Desktop.
Flutter - StreamBuilder with setState
This file contains 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 'package:flutter/material.dart'; | |
import 'dart:async'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith( | |
scaffoldBackgroundColor: darkBlue, | |
), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: MyHome(), | |
), | |
), | |
); | |
} | |
} | |
class MyHome extends StatefulWidget { | |
const MyHome({super.key}); | |
@override | |
State<MyHome> createState() => _MyHomeState(); | |
} | |
class _MyHomeState extends State<MyHome> { | |
StreamController<String> streamController = StreamController(); | |
int number = 0; | |
final Stream<int> _bids = (() { | |
late final StreamController<int> controller; | |
controller = StreamController<int>.broadcast( | |
onListen: () async { | |
await Future<void>.delayed(const Duration(seconds: 1)); | |
controller.add(1); | |
await Future<void>.delayed(const Duration(seconds: 1)); | |
controller.add(2); | |
await Future<void>.delayed(const Duration(seconds: 1)); | |
controller.add(4); | |
await Future<void>.delayed(const Duration(seconds: 1)); | |
await controller.close(); | |
}, | |
); | |
return controller.stream; | |
})(); | |
@override | |
initState(){ | |
super.initState(); | |
_bids.listen((val) => setState((){ | |
number = val; | |
})); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text("Iteration $number")), | |
body: StreamBuilder<int>( | |
stream: _bids, | |
builder: (BuildContext context, AsyncSnapshot<int> snapshot) { | |
List<Widget> children; | |
if (snapshot.hasError) { | |
children = <Widget>[ | |
const Icon( | |
Icons.error_outline, | |
color: Colors.red, | |
size: 60, | |
), | |
Padding( | |
padding: const EdgeInsets.only(top: 16), | |
child: Text('Error: ${snapshot.error}'), | |
), | |
Padding( | |
padding: const EdgeInsets.only(top: 8), | |
child: Text('Stack trace: ${snapshot.stackTrace}'), | |
), | |
]; | |
} else { | |
print("calling"); | |
// setState((){ | |
// number = snapshot.data!.toInt(); | |
// }); | |
switch (snapshot.connectionState) { | |
case ConnectionState.none: | |
children = const <Widget>[ | |
Icon( | |
Icons.info, | |
color: Colors.blue, | |
size: 60, | |
), | |
Padding( | |
padding: EdgeInsets.only(top: 16), | |
child: Text('Select a lot'), | |
), | |
]; | |
break; | |
case ConnectionState.waiting: | |
children = const <Widget>[ | |
SizedBox( | |
width: 60, | |
height: 60, | |
child: CircularProgressIndicator(), | |
), | |
Padding( | |
padding: EdgeInsets.only(top: 16), | |
child: Text('Awaiting bids...'), | |
), | |
]; | |
break; | |
case ConnectionState.active: | |
children = <Widget>[ | |
const Icon( | |
Icons.check_circle_outline, | |
color: Colors.green, | |
size: 60, | |
), | |
Padding( | |
padding: const EdgeInsets.only(top: 16), | |
child: Text('\$${snapshot.data}'), | |
), | |
]; | |
break; | |
case ConnectionState.done: | |
children = <Widget>[ | |
const Icon( | |
Icons.info, | |
color: Colors.blue, | |
size: 60, | |
), | |
Padding( | |
padding: const EdgeInsets.only(top: 16), | |
child: Text('\$${snapshot.data} (closed)'), | |
), | |
]; | |
break; | |
} | |
} | |
return Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: children, | |
), | |
); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment