-1

My Flutter application supports multiple platforms (Android, IOS, Windows, Web), How can I know the current Platform by the code?

I need to change some variables and URLs according to the Platform!

ADM
  • 20,406
  • 11
  • 52
  • 83
Ali Al-Amrad
  • 558
  • 5
  • 6

1 Answers1

1

To detect the current Platform (System):

You can use the built-in Platform from dart:io like the following:

import 'dart:io' show Platform;

if (Platform.isAndroid) {
  // Android-specific code
} else if (Platform.isIOS) {
  // iOS-specific code
}

As you can see, inside the Platform class there are a condition for each Platform, which are:

Platform.isAndroid
Platform.isFuchsia
Platform.isIOS
Platform.isLinux
Platform.isMacOS
Platform.isWindows

and for the Web, you can use the the constant kIsWeb from the flutter/foundation like the following:

import 'package:flutter/foundation.dart' show kIsWeb;

if (kIsWeb) {
  // running on the web!
}

References:

  1. This answer in Stack Overflow.
  2. Dart Platform class documentation.
Moaz El-sawaf
  • 2,306
  • 1
  • 16
  • 33