Flutter

Flutter - Internet Connection 확인

jaebijae 2024. 6. 1. 19:26

목차

    Internet Access

    • Internet Access가 가능한지 확인 필요`
    • `connectivity_plus` 패키지를 사용하려 했으나…. 
      • 기기가 모바일 데이터에서 실행되는지, Wifi에서 실행되는지 확인하는데는 유용
      • 실제 인터넷 엑세스를 확인하는데는 좋지 않음
      • 패키지 설명: `Note that on Android, this does not guarantee connection to Internet. For instance, the app might have WiFi access but it might be a VPN or a hotel WiFi with no Internet access.`
    • 해결 (임시):
      • Sample address로 socket을 열어 실제 연결 상태 확인….
      • 좋은 해결책인가? 아 몰랑
      Future<bool> get isConnected async {
        try {
          final result = await InternetAddress.lookup('example.com');
          if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
            return true;
          }
          return false;
        } on SocketException catch (_) {
          return false;
        }
      }