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!
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!
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: