해결된 질문
작성
·
1.6K
2
안녕하세요. 강의를 듣고나서 테스트를 해보는데 안되어서 문의 좀 드립니다.
addrinfo: json['addrinfo'] == null ? null : List<Contact_Item>.from(json['addrinfo']),
위 부분을 어떻게 처리해야 해결될 수 있는지를 모르겠습니다.
addrinfo 는 값이 서버에서 null을 반환할 수도 있고
List<Contact_Item> 을 반환할 수도 있습니다.
class Contact_Item {
final String idx;
final String userNM;
final String mobileNO;
final String telNO;
final String photo;
final bool checkBoxState;
const Contact_Item({
required this.idx,
required this.userNM,
required this.mobileNO,
required this.telNO,
required this.photo,
required this.checkBoxState,
});
factory Contact_Item.fromJson(Map<String, dynamic> json) {
return Contact_Item(
idx: json['idx'] as String,
userNM: json['userNM'] as String,
mobileNO: json['mobileNO'] as String,
telNO: json['telNO'] as String,
photo: json['photo'] as String,
checkBoxState: json['checkBoxState'] as bool,
);
}
}
class ContactResult {
final String status;
final String message;
final List<Contact_Item>? addrinfo;
const ContactResult({
required this.status,
required this.message,
this.addrinfo,
});
factory ContactResult.fromJson(Map<String, dynamic> json) {
return ContactResult(
status: json['status'] ?? '',
message: json['message'] ?? '',
addrinfo: json['addrinfo'] == null ? null : List<Contact_Item>.from(json['addrinfo']),
);
}
}
abstract class ContactRepo {
Future<ContactResult> getAddressData(ContactRequest req);
}
class ContactService extends ContactRepo {
Future<ContactResult> getAddressData(ContactRequest req) async {
BaseOptions options = BaseOptions(
baseUrl: RetrofitURL.baseUrl,
);
Dio dio = Dio(options);
dio.interceptors.add(Logging());
FormData formData = FormData.fromMap({
"keyword": req.keyword,
"search": req.search,
});
final response = await dio.post(RetrofitURL.contactData, data: formData);
print(response);
// print(response.data.runtimeType);
//print(response.headers);
if (response.statusCode == 200) {
ContactResult result = ContactResult.fromJson(response.data);
return result;
} else {
return ContactResult(status: "fail", message: "fail", addrinfo: null);
}
}
}
에러 메시지 내용
I/flutter (23550): RESPONSE[200] => PATH: /androidSample/ContactList.php
I/flutter (23550): {"status":"success","message":"","addrinfo":[{"idx":1,"userNM":"개발자","mobileNO":"01000010001","telNO":"0234560001","photo":"1.jpg","checkBoxState":false},{"idx":2,"userNM":"이정은","mobileNO":"01001230001","telNO":"","photo":"2.jpg","checkBoxState":false},{"idx":3,"userNM":"김홍길","mobileNO":"01001230002","telNO":"","photo":"null","checkBoxState":false},{"idx":4,"userNM":"최신형","mobileNO":"01001230003","telNO":"","photo":"4.jpg","checkBoxState":false},{"idx":5,"userNM":"홍길동","mobileNO":"01000009880","telNO":"","photo":"5.jpg","checkBoxState":false},{"idx":6,"userNM":"김아정","mobileNO":"01001230005","telNO":"","photo":null,"checkBoxState":false},{"idx":7,"userNM":"이순신","mobileNO":"01001230006","telNO":"","photo":"7.jpg","checkBoxState":false},{"idx":8,"userNM":"이정민","mobileNO":"01000010887","telNO":"","photo":null,"checkBoxState":false},{"idx":9,"userNM":"최재수","mobileNO":"01001110000","telNO":"","photo":"9.jpg","checkBoxState":false},{"idx":10,"userNM":"장정은"
E/flutter (23550): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type '_Map<String, dynamic>' is not a subtype of type 'Contact_Item'
E/flutter (23550): #0 new List.from (dart:core-patch/array_patch.dart:29:5)
E/flutter (23550): #1 new ContactResult.fromJson (package:login_ex/contact/model/contact_result.dart:19:51)
E/flutter (23550): #2 ContactService.getAddressData (package:login_ex/contact/repository/contact_service.dart:32:44)
E/flutter (23550): <asynchronous suspension>
E/flutter (23550): #3 MainScreenState.getContactData (package:loginex/contact/view/main_screen.dart:35:30)
E/flutter (23550): <asynchronous suspension>
답변 1
1
안녕하세요.
addrinfo의 타입을 List<Contact_Item>?
로 처리하는 경우
addrinfo: json['addrinfo'] == null ? null : json['addrinfo'].map<Contact_Item>((i) => Contact_Item.fromJson(i)).toList(),
addrinfo의 타입을 List<Contact_Item>
로 처리하고 싶은 경우
addrinfo: (json['addrinfo'] ?? []).map<Contact_Item>((i) => Contact_Item.fromJson(i)).toList(),
map 함수를 통하여 배열의 값을 하나씩 Contact_Item
으로 역직렬화 해주시면 됩니다.
감사합니다 :)
감사합니다. 이제 확실하게 이해했습니다.