title: Flutter: How to access 'provider' values in a function outside the build context of the child widget?
body: <p>The provider values are coming from the parent widget. I can use the provider values under the build context. However I need the provider values in the <strong>getHomeCampaigns</strong> function. I tried to define local variables and assign them to the provider values once the widget is built, but the function claims that the variables are called on null. I guess they are being used before they are being set under the build context from the provider.</p>
  
  <pre><code>class HomeScreen extends StatefulWidget {
    @override
    _HomeScreenState createState() =&gt; _HomeScreenState();
  }
  
  class _HomeScreenState extends State&lt;HomeScreen&gt; {
    // Variables to be set inside the function and then used by the last widget
    User currentUser;
    String country;
    List&lt;CampaignWidget&gt; friendsCamps;
    List&lt;CampaignWidget&gt; featuredCamps;
  
    // Function that needs the provider values
    getHomeCampaigns() async {
      // Get all campaigns where their owner is in the list of friends for the current user
      QuerySnapshot friendsSnapshot = await Firestore.instance
          .collection('campaigns')
          .where('user.uid', whereIn: currentUser.friends)
          .where('attributes.active', isEqualTo: true)
          .orderBy('created', descending: true)
          .limit(20)
          .getDocuments();
      // Get featured campaigns documents in the current country
      QuerySnapshot featuredSnapshot = await Firestore.instance
          .collection('campaigns')
          .where('attributes.featured', isEqualTo: true)
          .where('funders.${currentUser.uid}', isEqualTo: false)
          .where('country', isEqualTo: country)
          .orderBy('user.rating', descending: true)
          .limit(5)
          .getDocuments();
      // Make 2 lists of CampaignWidget out of the documents retrieved
      List&lt;CampaignWidget&gt; campaigns = friendsSnapshot.documents
          .map((doc) =&gt; CampaignWidget(campaign: Campaign.fromDocument(doc)))
          .toList();
      List&lt;CampaignWidget&gt; featured = featuredSnapshot.documents
          .map((doc) =&gt; CampaignWidget(campaign: Campaign.fromDocument(doc)))
          .toList();
      setState(() {
        // Set the featured and friends lists of CampaignWidget to the newly made lists
        this.featuredCamps = featured;
        this.friendsCamps = campaigns;
      });
    }
  
    @override
    void initState() {
      super.initState();
      getHomeCampaigns();
    }
  
    @override
    Widget build(BuildContext context) {
      this.currentUser = Provider.of&lt;User&gt;(context);
      this.country = Provider.of&lt;String&gt;(context);
      return Scaffold(
        backgroundColor: Color(0xFFE8E8E8),
        appBar: AppBar(
          centerTitle: false,
          title: Text("Home"),
          actions: &lt;Widget&gt;[
            /// Search users
            IconButton(
              icon: Icon(
                Icons.search,
              ),
              onPressed: () {},
            ),
          ],
        ),
        body: RefreshIndicator(
          onRefresh: () =&gt; getHomeCampaigns(),
          child: // Some widget that uses the variables,
        ),
      );
    }
  }
  </code></pre>