1. 백엔드 서버 데이터 주고 받을 시 에러 핸들링 하는 방법을 알아보자.
- grocery_list.dart
1)
String? _error;
final response = await http.get(url);
if (response.statusCode >= 400) {
setState(() {
_error = 'Failed to fetch data. Please try again later.';
});
}
--> statsCode가 400이상이면 응답에 에러가 있다는 것이다. 그러면 _error문자를 설정해준다.
그리고 아래와 같이 content에 적용해준다.
if (_error != null) {
content = Center(child: Text(_error!));
}
'코딩강의 > shopping_list(플러터-유데미)' 카테고리의 다른 글
228. Handling the "No Data" Case (0) | 2023.11.15 |
---|---|
227. Sending DELETE Requests (0) | 2023.11.15 |
225. Managing the Loading State (0) | 2023.11.15 |
224. Avoiding Unnecessary Requests (0) | 2023.11.15 |
~223. Fetching & Transforming Data (0) | 2023.11.15 |