Skip to content

Instantly share code, notes, and snippets.

@aelshamy
Forked from jtlapp/streamprovider_example.dart
Created January 11, 2020 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aelshamy/4468395bce5dd1f0ca133acd35ef19d2 to your computer and use it in GitHub Desktop.
Save aelshamy/4468395bce5dd1f0ca133acd35ef19d2 to your computer and use it in GitHub Desktop.
StreamProvider example
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
const appTitle = 'StreamProvider Demo';
return MaterialApp(
title: appTitle,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: StreamProvider<int>(
initialData: 0,
builder: (context) {
// Pretend this is loading data and reporting the percent loaded.
return Stream<int>
.periodic(Duration(milliseconds: 100), (count) => count + 1)
.take(100);
},
child: Scaffold(
appBar: AppBar(
title: Text(appTitle),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Consumer<int>(
builder: (context, percentDone, child) {
if (percentDone < 100) {
return Text("Loading... ($percentDone% done)");
}
return Text("Done loading!");
},
),
],
),
),
),
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment