๋คํธ์ํฌ ์์ฒญ์ ์ธํฐ๋ท ์ฐ๊ฒฐ ์ฌ๋ถ๋ฅผ ํ์ธํ๊ณ ์ธํฐ๋ท ์์ฒด๊ฐ ์ฐ๊ฒฐ์ด ๋์ด ์์ง ์์ ๊ฒฝ์ฐ ๋คํธ์ํฌ ์์ฒญ์ ์์ ๋ ๋ฆฌ์ง ์๊ณ ๋ฐ๋ก ์๋ฌ ์ฒ๋ฆฌ๋ฅผ ํ๊ธฐ ์ํจ์ด๋ค.
flutter pub add connectivity_plus
import 'package:connectivity_plus/connectivity_plus.dart';
class ConnectionManager {
final Connectivity connectivity;
ConnectionManager({
required this.connectivity,
});
Future<bool> get isConnected async {
final ConnectivityResult connectivityResult =
await connectivity.checkConnectivity();
return ((connectivityResult == ConnectivityResult.wifi) ||
(connectivityResult == ConnectivityResult.mobile));
}
}
ํ๋ก์ ํธ์ ์ฌ์ฉํ๋ ์ค์ ์ฝ๋๋ฅผ ๋ฐ์ทํด์๋ค. ํ์ฌ ํ
ํ๋ฆฟ ์ฝ๋๋ฅผ ๊ตฌํ์ค์ธ ๋ ํฌ์งํ ๋ฆฌ์๋ ์ ์ฝ๋(ConnectionManager) ๊น์ง๋ง ๊ตฌํํด๋์๋ค.
@override
Future<Either<AppException, MealSummaryModel>> getMealSummary(
int mealId) async {
bool isConnected = await connectionManager.isConnected;
if (!isConnected) {
return Left(AppException(TextManager.internetNotConnected.tr()));
}
try {
final response = await apiClient.get('/meal/summary/$mealId');
MealSummaryModel mealSummary =
MealSummaryModel.fromResponse(response.data);
return Right(mealSummary);
} on DioException catch (_) {
return Left(AppException(TextManager.mealFetchFailedMessage.tr()));
}
}