반응형
RESTApi는 거의 대부분 JSON으로 통신을 합니다.
아래 예시는 JSON을 이용해서 데이터 송 수신을 하는 예시입니다.
Future<String> Communication(User user) async {
var x = {"name" : "${user.name}", "email" : "${user.email}"};
var bd = jsonEncode(x); // 인코딩
try {
http.Response response = await http.post(
Uri.parse('엔드포인트 주소'),
headers: {"Content-Type": "application/json"}, // 필수
body: bd,
);
print(jsonDecode(response.body)); // 출력해보기
var D = (jsonDecode(response.body))['id']; // key 이름이 id인 것의 데이터는?
return D;
}
catch(e) {
print(e);
}
return "";
}
참고
반응형
'[클라이언트] > [Flutter]' 카테고리의 다른 글
[Flutter] ReorderableListView (ListView drag / drag ListView) (0) | 2022.10.31 |
---|---|
[Flutter] Firebase FCM 노티피케이션이 도착하지 않는다면 (0) | 2022.10.28 |
Flutter 기반의 Firebase 알림설정 다양한 케이스 정리 예정.. (0) | 2022.10.23 |
[Flutter] Unhandled Exception: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10 (0) | 2022.10.22 |
[Flutter] FireStore Unhandled Exception: PlatformException (0) | 2022.10.22 |