title: Flutter: Access SharedPreferences Provider / ChangeNotifier in a Stream Class
body: <p>I've looked around in StackoverFlow and was not able to find myself a solution to this.
  Scenario:
  I have a Flutter SharedPreferences Provider with ChangeNotifier Class, that will get updated with the current Logged In User info.</p>
  <p>Simplified content:</p>
  <pre><code>class SharedPreferences {
    final String userId;
    final String userName;
  
    SharedPreferences({
      @required this.userId,
      @required this.userName,
    });
  }
  class SharedPreferencesData with ChangeNotifier {
    var _sharedPreferencesData = SharedPreferences(
      userId: 'testUserId',
      userName: 'testUserName',
    );}
  </code></pre>
  <p>And a database.dart file with Class containing DataBaseServices to get FireStore Streams from Snapshots:</p>
  <pre><code>class DatabaseService {
    final CollectionReference companiesProfilesCollection =
        Firestore.instance.collection('companiesProfiles');
  
    List&lt;CompanyProfile&gt; _companiesProfilesFromSnapshot(QuerySnapshot snapshot) {
      return snapshot.documents.map((doc) {
        return CompanyProfile(
          docId: doc.documentID,
          companyName: doc.data['companyName'] ?? '',
          maxLocationsNumber: doc.data['maxLocationsNumber'] ?? 0,
          maxUsersNumber: doc.data['maxUsersNumber'] ?? 0,
        );
      }).toList();
    }
  
    Stream&lt;List&lt;CompanyProfile&gt;&gt; get getCompaniesProfiles {
      return companiesProfilesCollection
          .where('userId', isEqualTo: _userIdFromProvider) 
          // My problem is above -----
          .snapshots()
          .map(_companiesProfilesFromSnapshot);
    }
  }
  </code></pre>
  <p>I Don't want to fetch the entire Stream data as it could be massive for other Streams, I just want to pass the userID under .where('userId', isEqualTo:_userIdFromProvider).</p>
  <ul>
  <li><p>I couldn't access the context in this class to get the data from the Provider</p>
  </li>
  <li><p>Couldn't send the userId to getCompaniesProfiles getter, as getter don't take parameters</p>
  </li>
  <li><p>And if I convert this getter to a regular method, I wont be able to send the userID to it, as this has to run under <strong>void main() {runApp(MyApp());} / return MultiProvider(providers: [</strong>  and By then I cannot call fetch the sharedPreferences with a context that does not contain the provider info ...</p>
  </li>
  <li><p>Couldn't figure out how to receive the context as a constructor in this class, when I did, I got the following <em>Only static members can accessed in initializers</em> in class DatabaseService.</p>
  </li>
  </ul>
  <p>I'm still a beginner, so I would appreciate if you could share with me the best approach to handle this.
  Thank you!</p>
  <p>*********** Re-Edited by adding the below: **************
  I'm trying to implement the same scenario, here is my code:
  Main file:</p>
  <pre><code>return MultiProvider(
    providers: [
      ChangeNotifierProvider&lt;SpData&gt;(
        create: (context) =&gt; SpData(),
      ),
      ProxyProvider&lt;SpData, DS&gt;(
        create: (context) =&gt; DS(),
        update: (ctx, spData, previousDS) {
          print('ChangeNotifierProxyProvider RAN');
          previousDS.dbData = spData;
          return previousDS;
        },
      ),
    ],
  </code></pre>
  <p>SP File:</p>
  <pre><code>class SP {
    final String companyId;
    SP({
      @required this.companyId,
    });
  }
  
  class SpData with ChangeNotifier {
    var _sPData = SP(
      companyId: '',
    );
    void setCompanyId(String cpID) {
      final newSharedPreferences = SP(
        companyId: cpID,
      );
      _sPData = newSharedPreferences;
      print('_spData companyId:${_sPData.companyId}');
      notifyListeners();
    }
  
    String get getCompanyId {
      return _sPData.companyId;
    }
  }
  </code></pre>
  <p>DS file:</p>
  <pre><code>class DS with ChangeNotifier {
    SpData dbData;
    void printCompanyId() {
      var companyId = dbData.getCompanyId;
      print('companyId from DataBase: $companyId');
    }
  }
  </code></pre>
  <p>The SpData dbData; inside Class DS does not update. I've added the prints to figure out what is running and what is not. When I run my code, the print function in main.dart file print('ChangeNotifierProxyProvider RAN'); does not run.</p>
  <p>What am I missing? Why ChangeNotifierProxyProvider is not being triggered, to update dbData inside DS file? Thanks!</p>