Created
September 18, 2019 04:07
-
-
Save jtlapp/bbcc6d24f7f528ab9a651e775d8ad000 to your computer and use it in GitHub Desktop.
FutureProvider example
This file contains hidden or 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 '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!"); | |
}, | |
), | |
], | |
), | |
), | |
), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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