getIpAddress method
Retrieves the public IP address of the device.
Uses an external service to get the public IP address. If the service is unreachable or an error occurs, it returns 'Unknown'.
Implementation
Future<String> getIpAddress() async {
try {
const url = 'https://api.ipify.org';
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
return response.body;
} else {
debugPrint(
'Failed to retrieve IP. Status code: ${response.statusCode}');
return 'Unknown';
}
} catch (e) {
debugPrint('Error fetching public IP: $e');
return 'Unknown';
}
}