State Management Handle shared Firebase data with Provider

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

Note: The code below is updated for Provider version 3.1. The main change is that the stream param has been renamed to value.

Example Code

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

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

class UserModel {
 String name = 'Bob';
}

var stream = Stream.fromIterable([UserModel()]);

class MyApp extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
   return StreamProvider<UserModel>.value(
     value: stream,
     child: MaterialApp(
       home: HomeScreen(),
     ),
   );
 }
}

class HomeScreen extends StatelessWidget {
 final Firestore db = Firestore.instance;

 @override
 Widget build(BuildContext context) {

   var user = Provider.of<UserModel>(context);

   return Scaffold(
     body: Center(
       child: Text(user.name),
     ),
   );
 }
}

Q&A Chat