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.

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),
     ),
   );
 }
}

Questions?

Ask questions via GitHub below OR chat on Slack #questions