Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.