How to: How to get Hostname in Dart| Flutter By Example

Sometimes, We need to get the hostname of a running application in the current environment in the dart and flutter application

This post shows multiple ways to get a hostname in the dart and flutter project.

How to get Hostname in the dart application

dart:io package provides platform🔗 class that provides information of an running environment.

Platform class has a static method localHostname that returns the hostname of a system.

static String get localHostname => _localHostname;

Here is an example program to get a hostname in dart

import 'dart:io' show Platform, stdout;

void main() {
  final hostname = Platform.localHostname; // returns hostname as string
  print(hostname);
}

Output

localhost

hostname print in flutter application

dart:html package provides html window class that contains location hostname properties html.window.location.hostname returns the hostname of the running machine.

import 'dart:html' as html;

void main() {
  final hostname = html.window.location.hostname;

  print(hostname);
}

Output:

localhost

Conclusion

Learned how to get the hostname of a running application in the dart and flutter app