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?

#1
For UI that should differ slightly on **iOS** and **Android**, i.e. on ***different platforms***, there must be a way to detect which one the app is running on, but I couldn't find it in the docs. What is it?
Reply

#2
Thanks to Collin, the final answer is:

bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
Reply

#3
You can do

defaultTargetPlatform == TargetPlatform.iOS
? kIOSTheme
: kDefaultTheme,

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

#4
Most "Flutter" answer is as follows:

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

//...

if(Theme.of(context).platform == TargetPlatform.android)
//do sth for Android
else if(Theme.of(context).platform == TargetPlatform.iOS)
//do sth else for iOS
else if(Theme.of(context).platform == TargetPlatform.fuchsia)
//even do sth else for Fuchsia OS
Reply

#5
```dart
import 'dart:io' show Platform; //at the top

String os = Platform.operatingSystem; //in your code
print(os);
```
Reply

#6
You can use Universal Platform package:

[To see links please register here]



import 'package:universal_platform/universal_platform.dart';

bool isIos = UniversalPlatform.isIOS;
bool isAndroid = UniversalPlatform.isAndroid;
bool isWeb = UniversalPlatform.isWeb;
print('iOS: $isIos');
print('Android: $isAndroid');
print('Web: $isWeb');





Reply

#7
It is simple just import the io library

import'dart:io' show Platform;
void main(){
if(Platform.isIOS){
return someThing();
}else if(Platform.isAndroid){
return otherThing();
}else if(Platform.isMacOS){
return anotherThing();
}

or in very simple way

Platform.isIOS ? someThing() : anOther(),

Reply

#8
for more simple way for web and app both.try this




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


var platformName = '';
if (kIsWeb) {
platformName = "Web";
} else {
if (Platform.isAndroid) {
platformName = "Android";
} else if (Platform.isIOS) {
platformName = "IOS";
} else if (Platform.isFuchsia) {
platformName = "Fuchsia";
} else if (Platform.isLinux) {
platformName = "Linux";
} else if (Platform.isMacOS) {
platformName = "MacOS";
} else if (Platform.isWindows) {
platformName = "Windows";
}
}
print("platformName :- "+platformName.toString());

Reply

#9


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

Don't forget to import IO Library.

import 'dart:io';

If you are using `import 'dart:html';` too in same file then you have to specify Platform definition as both libraries has definition of "Platform"

in that case use Platform Specific Code like Below:

import 'dart:io' as IO;
import 'dart:html';

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

If you are looking into Platform Integration properly I would Suggest Use Complete Example on Flutter site:
[

[To see links please register here]

][1]


[1]:

[To see links please register here]

Reply

#10
import 'dart:io' as io;

if(io.Platform.isAndroid){
doSomething();
}else {
doSomethingElse();
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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