viruchith
viruchith

Reputation: 61

How to get the IP address of a domain in Dart?

I want to create an app which gets the IP address of the url entered by the user (eg: www.google.com --> 216.58.197.78 ) using flutter and I came across the InternetAddress class but I have no idea how to use it and I am not whether it is what I am searching for . Please help me with this . Thanks in advance.

Upvotes: 1

Views: 2361

Answers (3)

conan
conan

Reputation: 1

conan@conan-ubuntu-desktop:~$ lsb_release -a

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.3 LTS
Release:    22.04
Codename:   jammy

conan@conan-ubuntu-desktop:~$ flutter --version

Flutter 3.13.0 • channel stable • https://github.com/flutter/flutter.git
Framework • revision efbf63d9c6 (2주 전) • 2023-08-15 21:05:06 -0500
Engine • revision 1ac611c64e
Tools • Dart 3.1.0 • DevTools 2.25.0
String sIP = "";
InternetAddress.lookup("google.com").then((value) {
  for (var element in value) {
     if (element.type==InternetAddressType.IPv4) {     //<-- IPv6, unix
       sIP = element.address;
       break; 
     }
  }
});

Upvotes: 0

Shohruhbek Bekjonov
Shohruhbek Bekjonov

Reputation: 86

Try this:

InternetAddress.lookup("google.com").then((value) {
  value.forEach((element) async {
    print(element.address);
  });
});

Upvotes: 7

Mr Random
Mr Random

Reputation: 2218

Use ipfinder

import 'package:ipfinder/ipfinder.dart';

void main() async {
  Ipfinder ipfinder = Ipfinder("YOUR_TOKEN_GOES_HERE");
  IpResponse ip = await ipfinder.getAddressInfo("1.0.0.0");
  print(ip.toJson()); // print json data
}

Upvotes: 1

Related Questions