content: import 'dart:async';
  import 'package:flutter/foundation.dart';
  import 'package:flutter/material.dart';
  import 'package:google_sign_in/google_sign_in.dart';
  import 'package:firebase_auth/firebase_auth.dart';
  import 'package:cloud_firestore/cloud_firestore.dart';
  
  import 'package:recipes_app/model/state.dart';
  import 'package:recipes_app/utils/auth.dart';
  
  class StateWidget extends StatefulWidget {
    final StateModel state;
    final Widget child;
  
    StateWidget({
      @required this.child,
      this.state,
    });
  
    // Returns data of the nearest widget _StateDataWidget
    // in the widget tree.
    static _StateWidgetState of(BuildContext context) {
      return (context.inheritFromWidgetOfExactType(_StateDataWidget)
              as _StateDataWidget)
          .data;
    }
  
    @override
    _StateWidgetState createState() => new _StateWidgetState();
  }
  
  class _StateWidgetState extends State<StateWidget> {
    StateModel state;
    GoogleSignInAccount googleAccount;
    final GoogleSignIn googleSignIn = new GoogleSignIn();
  
    @override
    void initState() {
      super.initState();
      if (widget.state != null) {
        state = widget.state;
      } else {
        state = new StateModel(isLoading: true);
        initUser();
      }
    }
  
    Future<Null> initUser() async {
      googleAccount = await getSignedInAccount(googleSignIn);
      if (googleAccount == null) {
        setState(() {
          state.isLoading = false;
        });
      } else {
        await signInWithGoogle();
      }
    }
  
    Future<List<String>> getFavorites() async {
      DocumentSnapshot querySnapshot = await Firestore.instance
          .collection('users')
          .document(state.user.uid)
          .get();
      if (querySnapshot.exists &&
          querySnapshot.data.containsKey('favorites') &&
          querySnapshot.data['favorites'] is List) {
        // Create a new List<String> from List<dynamic>
        return List<String>.from(querySnapshot.data['favorites']);
      }
      return [];
    }
  
    Future<Null> signInWithGoogle() async {
      if (googleAccount == null) {
        // Start the sign-in process:
        googleAccount = await googleSignIn.signIn();
      }
      FirebaseUser firebaseUser = await signIntoFirebase(googleAccount);
      state.user = firebaseUser; // new
      List<String> favorites = await getFavorites(); // new
      setState(() {
        state.isLoading = false;
        state.favorites = favorites; // new
      });
    }
  
    Future<Null> signOutOfGoogle() async {
      // Sign out from Firebase and Google
      await FirebaseAuth.instance.signOut();
      await googleSignIn.signOut();
      // Clear variables
      googleAccount = null;
      state.user = null;
      setState(() {
        state = StateModel(user: null);
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return new _StateDataWidget(
        data: this,
        child: widget.child,
      );
    }
  }
  
  class _StateDataWidget extends InheritedWidget {
    final _StateWidgetState data;
  
    _StateDataWidget({
      Key key,
      @required Widget child,
      @required this.data,
    }) : super(key: key, child: child);
  
    // Rebuild the widgets that inherit from this widget
    // on every rebuild of _StateDataWidget:
    @override
    bool updateShouldNotify(_StateDataWidget old) => true;
  }
  