메서드가 Dart에서 null을 반환하는 대신 http 응답을 기다리는 지 어떻게 확인할 수 있습니까?

루카

Facebook API로 작업하기 위해 Dart에 작은 명령 줄 라이브러리를 작성하려고합니다. auth-token 및 user-id를 속성으로 가져 오는 'fbuser'클래스가 있으며 사용자의 모든 그룹 ID가 포함 된 List를 반환해야하는 'groupIds'메서드가 있습니다.

메서드를 호출하면 http 응답 후에 두 개의 가능한 반환이 호출되지만 null을 반환합니다. 내가 뭘 잘못 했어?

내 코드는 다음과 같습니다.

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<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
}

현재 출력은 다음과 같습니다.

Observatory listening on http://127.0.0.1:56918
null
response received
174 Gruppen gefunden

이 메서드는 http 요청을 실행하지만 즉시 null을 반환합니다.

(올 여름에 프로그래밍을 시작했습니다. 도와 주셔서 감사합니다.)

Günter Zöchbauer
return http.get(url) // add return
void main() {
  var usr = new fbuser(123, 'XYY'); //construct user
  usr.groupIds().then((x) => print(x)); //call method to get the IDs
  // or
  usr.groupIds().then(print); //call method to get the IDs
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관