Flutter: can I find out what platform I am on?

Viewed 1985

I am developping a project that should work both as an app and a web page. I am using flutter_secure_storage to store local data for the app, but I need a different approach for web. Is there a way I can find out what platform I am on at runtime, and choose the correct read-write functions accordingly?

4 Answers

you can add this extension file to the project and call in any object

target.dart

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

extension Target on Object {
  bool isAndroid() {
    return Platform.isAndroid;
  } 
  bool isIOS() {
    return Platform.isIOS;
  } 
  bool isLinux() {
  return Platform.isLinux;
  } 
  bool isWindows() {
  return Platform.isWindows; 
  }
  bool isMacOS() {
  return Platform.isMacOS; 
  }
  bool isWeb() {
  return kIsWeb; 
  }
  // ···
}

and import it anywhere and use it like this

@override
  Widget build(BuildContext context) {
    return isAndroid()? Text("Android"):Text("Not Android");
  }

for web, you use:

if(kIsWeb) //returns a boolean if your app is running in a browser.

For other platform, you use Platform

Platform.isAndroid
Platform.isIos
Platform.isLinux

Make sure to check that you are running on web first, before calling Platform.is, because it'll throw an exception that platform is not available for this environment.

How to detect what platform a Flutter app is running on: Flutter provides two different APIs that enables the caller to get to know more about the current platform: the kIsWeb constant that is part of the foundation library and the Platform class being part of the platform library. Link to code: https://www.flutterclutter.dev/flutter/tutorials/how-to-detect-what-platform-a-flutter-app-is-running-on/2020/127/

 import 'dart:io';
 import 'package:flutter/foundation.dart' show kIsWeb;
 class PlatformInfo {
 bool isDesktopOS() {
  return Platform.isMacOS || Platform.isLinux || Platform.isWindows;
 }
 bool isAppOS() {
 return Platform.isMacOS || Platform.isAndroid;
  }
 bool isWeb() {
 return kIsWeb;
}
 PlatformType getCurrentPlatformType() {
  if (kIsWeb) {
     return PlatformType.Web;
}
  if (Platform.isMacOS) {
  return PlatformType.MacOS;
}
  if (Platform.isFuchsia) {
  return PlatformType.Fuchsia;
}
if (Platform.isLinux) {
  return PlatformType.Linux;
}
if (Platform.isWindows) {
  return PlatformType.Windows;
}
if (Platform.isIOS) {
  return PlatformType.iOS;
}
if (Platform.isAndroid) {
  return PlatformType.Android;
}
return PlatformType.Unknown;
}
}
enum PlatformType {
 Web,
 iOS,
 Android,
 MacOS,
 Fuchsia,
 Linux,
 Windows,
 Unknown
 }

This gives an easy possiblity to make decisions based on the OS or the group of underlying platforms:

Text _getMicrophoneSection() {
 if (PlatformInfo.isAppOS()) {
  return Text('Please tap the microphone button and then permit the              usage');
  }
 return Text('If you have a microphone available, please turn it on');

}

In order to detect if Flutter is compiled for web you may use kIsWeb from package import 'package:flutter/foundation.dart'. For other environments like Android, IOS, Windows, Mac you may use Platform class like

Platform.IsWindows

Related