title: How can I make sure a method waits for a http response instead of returning null in Dart?
body: <p>I am trying to write a little command line library in Dart to work with the Facebook API. I have a class 'fbuser' which gets the auth-token and user-id as properties and has a method 'groupIds' which should return a List with all IDs of groups from the user.</p>
  
  <p>When I call the method it returns null altough the two possbile returns are called after the http response. What did I do wrong?</p>
  
  <p>Here is my code:</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'dart:convert'; //used to convert json 
  import 'package:http/http.dart' as http; //for the http requests
  
  //config
  final fbBaseUri = "https://graph.facebook.com/v2.1";
  final fbAppID = "XXX";
  final fbAppSecret = "XXY";
  
  class fbuser {
    int fbid;
    String accessToken;
  
    fbuser(this.fbid, this.accessToken); //constructor for the object
  
    groupIds(){ //method to get a list of group IDs
      var url = "$fbBaseUri/me/groups?access_token=$accessToken"; //URL for the API request
      http.get(url).then((response) {
        //once the response is here either process it or return an error
        print ('response received');
        if (response.statusCode == 200) {
          var json = JSON.decode(response.body);
          List groups=[];
          for (int i = 0; i&lt;json['data'].length; i++) {
            groups.add(json['data'][i]['id']);
          }
          print(groups.length.toString()+ " Gruppen gefunden");
          return groups; //return the list of IDs
        } else {
          print("Response status: ${response.statusCode}");
          return (['error']); //return a list with an error element
        }
      });
    }
  }
  
  void main() { 
    var usr = new fbuser(123, 'XYY'); //construct user
    print(usr.groupIds()); //call method to get the IDs
  }
  </code></pre>
  
  <p>At the moment the output is:</p>
  
  <pre><code>Observatory listening on http://127.0.0.1:56918
  null
  response received
  174 Gruppen gefunden
  </code></pre>
  
  <p>The method runs the http request but it returns null immediately.</p>
  
  <p>(I started programming this summer. Thanks for your help.)</p>