Skip to content

Instantly share code, notes, and snippets.

@jtlapp
Created September 18, 2019 04:07
Show Gist options
  • Save jtlapp/bbcc6d24f7f528ab9a651e775d8ad000 to your computer and use it in GitHub Desktop.
Save jtlapp/bbcc6d24f7f528ab9a651e775d8ad000 to your computer and use it in GitHub Desktop.
FutureProvider 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 = 'FutureProvider Demo';
return MaterialApp(
title: appTitle,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: FutureProvider<bool>(
initialData: true,
builder: (context) {
// Pretend we're saving data and it takes 4 seconds.
return Future.delayed(Duration(seconds: 4), () => false);
},
child: Scaffold(
appBar: AppBar(
title: Text(appTitle),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Consumer<bool>(
builder: (context, saving, child) {
return Text(saving ? "Saving..." : "Saved!");
},
),
],
),
),
),
));
}
}
@tarikwaleed
Copy link

In the consumer widget, we're not specifying what provider we're receiving the value from.. how does the widget knows that we're receiving value from this future provider?

is that because we have only one FutureProvider in the widget tree? so the Consumer widget is smart enough that this is the

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment