Skip to content

Instantly share code, notes, and snippets.

@brianegan
Created October 12, 2018 15:44
Show Gist options
  • Save brianegan/8ed4a5155b5f50412dbf420c67ce2772 to your computer and use it in GitHub Desktop.
Save brianegan/8ed4a5155b5f50412dbf420c67ce2772 to your computer and use it in GitHub Desktop.
Even using a Sync Stream Controller + Buffering an event, the StraemBuilder will print "No Data" then "Has Data"
import 'dart:async';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
MyAppState createState() {
return new MyAppState();
}
}
class MyAppState extends State<MyApp> {
final controller = StreamController<String>(sync: true)..add("Hello");
@override
void dispose() {
controller.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(
title: 'Flutter Demo Home Page',
stream: controller.stream,
),
);
}
}
class MyHomePage extends StatelessWidget {
final String title;
final Stream<String> stream;
const MyHomePage({Key key, this.title, this.stream}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: StreamBuilder(
stream: stream,
builder: (context, snapshot) {
print("Has Data? ${snapshot.hasData}");
return Container();
}),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment