Async Widgets Handle Streams and Futures

This lesson is available for PRO members or as a single course purchase. Sign-in and choose a plan below.


*Enrollment provides full access to this course (and updates) for life.

Or Signup for Unlimited PRO Access

Example Code

file_type_dartlang main.dart
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:async';



void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
   return MaterialApp(
     home: HomeScreen(),
   );
 }
}

class HomeScreen extends StatelessWidget {

 final Firestore db = Firestore.instance;

 @override
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(title: Text('Home')),
     body: Center(
       child: StreamBuilder<DocumentSnapshot>(
         stream: db.collection('users').document('mB6sGaFBczfIW50DJyvGDcQWOvW2').snapshots(),
         builder: (context, snapshot) {
          
           if (snapshot.hasData) {

             var data = snapshot.data.data;

             return Column(
               crossAxisAlignment: CrossAxisAlignment.center,
               mainAxisAlignment: MainAxisAlignment.spaceEvenly,
               children: <Widget>[

                 Image.network(data['photoURL']),
                 Text(data['username'], style: Theme.of(context).textTheme.display1,),


               ],
             );
           } else {
             return Container();
           }

          
         }
       ),
     )
   );
 }
}

Q&A Chat