title: Why is Flutter Future not waiting
body: <p>I'm still trying to understand how future works in dart, I wrote a method, that fetches data from an API like so:</p>
  
  <pre><code>Future&lt;CountryData&gt; getCountryData(String country) async {
      CountryData data;
      await apiService.getCountryData(country).then((country) {
          data = country;
      });
        return data;
    } 
  </code></pre>
  
  <p>But when calling the method it does not wait for execution to complete. This is how the method is being called:</p>
  
  <pre><code>Future&lt;void&gt; _updateData() async {
      try {
        final dataRepository =
            Provider.of&lt;DataRepository&gt;(context, listen: false);
            **final endpointcountrydata = await dataRepository.getCountryData("nigeria");**
             final africaData = await dataRepository.getAfricaData();
        setState(() {
          _endpointsData = endpointsData;
          _countryData = endpointcountrydata;
          _africaModel = africaData;
        });
  
      } on SocketException catch (_) {
        showAlertDialog(
          context: context,
          title: 'Connection Error',
          content: 'Could not retrieve data. Please try again later.',
          defaultActionText: 'OK',
        );
      } catch (_) {
        showAlertDialog(
          context: context,
          title: 'Unknown Error',
          content: 'Please contact support or try again later.',
          defaultActionText: 'OK',
        );
      }
    }
  </code></pre>
  
  <p>Both methods (endpointcountrydata and africaData) don't wait for exceution to complete.</p>
  
  <p>The _updateData() method is called on the initState like so:</p>
  
  <pre><code>@override
    void initState() {
      super.initState();
      final dataRepository = Provider.of&lt;DataRepository&gt;(context, listen: false);
  
  
       **dataRepository.getAfricaData().then((value){
         print(value);
       });**
  
      _updateData();
    }
  </code></pre>
  
  <p>I even tried calling the method (getAfricaData()) from initState, still not waiting.</p>
  
  <p>where am I getting it wrong?</p>
  
  <p>From answers below, I have tried this:</p>
  
  <pre><code>Future&lt;CountryData&gt; getCountryData(String country) async {
      CountryData data = await apiService.getCountryData(country);
        return data;
    } 
  </code></pre>
  
  <p>Then I instatiated a bool like so:</p>
  
  <pre><code>bool _isFetchingData;
  </code></pre>
  
  <p>This is how the _updateData method looks like:</p>
  
  <pre><code>  Future&lt;void&gt; _updateData() async {
      try {
        final dataRepository =
            Provider.of&lt;DataRepository&gt;(context, listen: false);
            final endpointcountrydata = await dataRepository.getCountryData("nigeria");
        final endpointsData = await dataRepository.getAllEndpointsData();
        final africaData = await dataRepository.getAfricaData();
  
        setState(() {
          _isFetchingData = true;
          _endpointsData = endpointsData;
          _countryData = endpointcountrydata;
          _africaModel = africaData;
        });
        if(_countryData != null &amp;&amp; _africaModel != null){
          setState(() {
            _isFetchingData = false;
          });
        }
      } on SocketException catch (_) {
        showAlertDialog(
          context: context,
          title: 'Connection Error',
          content: 'Could not retrieve data. Please try again later.',
          defaultActionText: 'OK',
        );
      } catch (_) {
        showAlertDialog(
          context: context,
          title: 'Unknown Error',
          content: 'Please contact support or try again later.',
          defaultActionText: 'OK',
        );
      }
    }
  </code></pre>
  
  <p>Then Initstate:</p>
  
  <pre><code>  @override
    void initState() {
      super.initState();
      final dataRepository = Provider.of&lt;DataRepository&gt;(context, listen: false);
      _endpointsData = dataRepository.getAllEndpointsCachedData();
  
      _updateData();
    }
  </code></pre>
  
  <p>Yet, still not waiting. Pulling my hair out already.</p>