Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 907 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you detect the host platform from Dart code?

#11
So here is a little utility I use to detect platforms and os to be reactive as appropriate.

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

class Util {

os getPlatform() {
if (kIsWeb) {
return os.Web;
} else if (Platform.isIOS) {
return os.IOS;
} else if (Platform.isAndroid) {
return os.Android;
} else if (Platform.isFuchsia) {
return os.Fuchsia;
} else if (Platform.isLinux) {
return os.Linux;
} else if (Platform.isMacOS) {
return os.MacOS;
} else if (Platform.isWindows) {
return os.Windows;
}
return os.Unknown;
}

bool isWeb() {
return (getPlatform()==os.Web);
}

bool isMobile() {
os platform = getPlatform();
return (platform == os.Android || platform == os.IOS || platform== os.Fuchsia);
}

bool isComputer() {
os platform = getPlatform();
return (platform == os.Linux || platform == os.MacOS || platform== os.Windows);
}

}

enum os { Unknown, Web, Android, Fuchsia, IOS, Linux, MacOS, Windows }

```
Reply

#12
If you just need a string for logging purposes, you can use `Platform.operatingSystem`, which returns the OS name as a lowercase string.

```dart
import 'dart:io';
import 'package:flutter/foundation.dart';

String _getPlatform() {
if (kIsWeb) return 'web';
return Platform.operatingSystem;
}
```
Reply

#13
Although [`defaultTargetPlatform`][1] will work, I would suggest using `Theme.of(context).targetPlatform`. This enables testing of iOS behavior (because `defaultTargetPlatform` is always `TargetPlatform.android` in tests). It also allows ancestors of your widget to override its target platform by wrapping it in a `Theme` widget.

[1]:

[To see links please register here]

Reply

#14
<!-- language: lang-dart -->

import 'dart:io' show Platform;

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

All options include:
<!-- language: lang-dart -->

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

You can also detect if you are running on the web using `kIsWeb`, a global constant indicating if the application was compiled to run on the web:
<!-- language: lang-dart -->

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

if (kIsWeb) {
// running on the web!
} else {
// NOT running on the web! You can check for additional platforms here.
}


- `Platform` documentation:

[To see links please register here]

- `kIsWeb` documentation:

[To see links please register here]

Reply

#15
Checking Host Platform in Dart.

import 'dart:io' as IO;

_checkingHostPlatform(){
if(IO.Platform.isAndroid){
//Execute code for android
}else if(IO.Platform.isIOS){
//Execute code for iOS
}else{
//Execute code for other platforms
}
}
Reply

#16
import 'dart:io' show Platform;

if (Platform.isAndroid) {
// Android-specific code
} else if (Platform.isIOS) {
// iOS-specific code
}else if (Platform.isFuchsia) {
// Fuchsia-specific code
}else if (Platform.isLinux) {
// Linux-specific code
}else if (Platform.isMacOS) {
// MacOS-specific code
}else if (Platform.isWindows) {
// Windows-specific code
}else if (Platform.isWindows) {
// Windows-specific code
}

> for web

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

if (kIsWeb) {
// running on the web!
} else {
// NOT running on the web! You can check for additional platforms here.
}

Reply

#17
you can add this extension file (platform_ext.dart) to the project and call in any object

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;
}
// ···
}

just import the file and call it



import 'platform_ext.dart';
....
@override
Widget build(BuildContext context) {
return isAndroid()? Text("Android"):Text("Not Android");
}
Reply

#18
**multiple platform you check and run your code according specific platform **

bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
bool isAndroid = Theme.of(context).platform == TargetPlatform.android;
bool islinux = Theme.of(context).platform == TargetPlatform.linux;
bool isfuchsia = Theme.of(context).platform == TargetPlatform.fuchsia;
bool isMacOS = Theme.of(context).platform == TargetPlatform.macOS;
bool iswindows = Theme.of(context).platform == TargetPlatform.windows;
Reply

#19
You can add this extensions to your project

```
import 'package:flutter/material.dart';

extension PlatformExtension on BuildContext {
bool get isMobile =>
Theme.of(this).platform == TargetPlatform.iOS ||
Theme.of(this).platform == TargetPlatform.android;

bool get isDesktop =>
Theme.of(this).platform == TargetPlatform.macOS ||
Theme.of(this).platform == TargetPlatform.windows ||
Theme.of(this).platform == TargetPlatform.linux;
}

extension TargetPlatformExtension on TargetPlatform {
bool get isMobile =>
this == TargetPlatform.iOS || this == TargetPlatform.android;

bool get isDesktop =>
this == TargetPlatform.linux ||
this == TargetPlatform.macOS ||
this == TargetPlatform.windows;
}

```

Now you can access the host platform using.

1. BuildContext => context.isDesktop

2. TargetPlatform => defaultTargetPlatform.isDesktop

It is recommended to access the platform from context.

To detect if your app is running on browsers, you can easily use the kIsWeb constant from the foundation library.

```
import 'package:flutter/foundation.dart';

log('$kIsWeb'); // will print true if the app is running on a browser
```

Note that if you are running a web app on a desktop, the isDesktop getter will return true, and same thing for mobile platforms.
And to avoid that.

```
import 'package:flutter/foundation.dart';

if (!kIsWeb && defaultTargetPlatform.isDesktop) {
// do stuff for desktop apps only
}

if (!kIsWeb && defaultTargetPlatform.isMobile) {
// do stuff for mobile apps only
}
```

You can modify the extensions to get your preferred implementation.
Reply

#20
If you import both ***"dart:io"*** and ***"dart:html"***, it does not understand which Platform to import and gives error. So import one of them.

import 'dart:io';
Platform.isIOS ? CupertinoWidget() : MaterialWidget()
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through