title: How to change the stream in StreamProvider in Flutter
body: <p>I'm consuming a stream from Firebase Firestore with a <code>StreamProvider</code> from the <code>provider</code>-package like the below code snippet shows (<code>DatabaseService.streamVehicles</code> is just a wrapper function to the actual query to Firestore).</p>
  
  <p>In response to user events, the <code>locationId</code> variable will be set to different ids to filter in the Firestore query. However, whenever <code>locationId</code> changes, the component will rebuild, but the function passed to the <code>create</code>-parameter will not be called again, so the new value of <code>locationId</code> will not be passed to the query and the stream is still the same, unfiltered stream. How can I make sure a new stream is created whenever <code>locationId</code> changes?</p>
  
  <pre class="lang-dart prettyprint-override"><code>class HomePage extends StatefulWidget {
    static final String id = 'home';
  
    @override
    _HomePageState createState() =&gt; _HomePageState();
  }
  
  class _HomePageState extends State&lt;HomePage&gt; {
    final CupertinoTabController _tabController =
        CupertinoTabController(initialIndex: 0);
  
    String locationId;
  
    void setLocationId(String newId) {
      this.setState(() {
        locationId = newId;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      print("Building homepage");
      return Consumer&lt;User&gt;(
        builder: (context, user, _) {
          return StreamProvider&lt;List&lt;Vehicle&gt;&gt;(
            create: (context) =&gt; DatabaseService.streamVehicles(
              user.organization.id,
              locationId: locationId,
            ),
            catchError: (ctx, err) {
              print("Error in stream");
              print(err);
              return [];
            },
            child: CupertinoTabScaffold(
              controller: _tabController,
              tabBar: CupertinoTabBar(
                activeColor: kHighlightColor,
                items: &lt;BottomNavigationBarItem&gt;[
                  BottomNavigationBarItem(
                    icon: Icon(CupertinoIcons.collections),
                    title: Text('Vehicles'),
                  ),
                  BottomNavigationBarItem(
                    icon: Icon(CupertinoIcons.settings),
                    title: Text('Settings'),
                  ),
                ],
              ),
              tabBuilder: (BuildContext context, int index) {
                switch (index) {
                  case 0:
                    return VehiclesPage(
                        locationId: locationId, setLocationId: setLocationId);
                  case 1:
                    return SettingsPage();
                }
                return null;
              },
            ),
          );
        },
      );
    }
  }
  </code></pre>
  
  <p>The <code>streamVehicles</code> method of <code>DatabaseService</code> is defined as the following.</p>
  
  <pre class="lang-dart prettyprint-override"><code>  static Stream&lt;List&lt;Vehicle&gt;&gt; streamVehicles(
      String organizationId, {
      String locationId,
    }) {
      final Query ref = _firestore
          .collection('organizations')
          .document(organizationId)
          .collection('vehicles')
          .where(
            "location.id",
            isEqualTo: locationId,
          );
  
      return ref.snapshots().map((vehicleSnapshots) =&gt; vehicleSnapshots.documents
          .map((vehicleSnapshot) =&gt; Vehicle.fromFirestore(vehicleSnapshot))
          .toList());
    }
  </code></pre>