Flutter web platform check
Asked Answered
C

3

6

I am trying to find a way to see which platform (iOS or Android) my flutter web project is running on. When I use if (Platform.isIOS) { //do something }, I face error below:

Error: Unsupported operation: Platform._operatingSystem
at Object.throw_ [as throw] (http://localhost:44159/dart_sdk.js:4334:11)
at Function._operatingSystem (http://localhost:44159/dart_sdk.js:55064:17)
at Function.get operatingSystem [as operatingSystem] (http://localhost:44159/dart_sdk.js:55110:27)
at get _operatingSystem (http://localhost:44159/dart_sdk.js:55023:27)
at Function.desc.get [as _operatingSystem] (http://localhost:44159/dart_sdk.js:4819:15)
at get isIOS (http://localhost:44159/dart_sdk.js:55047:26)
at Function.desc.get [as isIOS] (http://localhost:44159/dart_sdk.js:4819:15)
at http://localhost:44159/packages/itoll/main.dart.lib.js:364:33
at wrapper (http://localhost:44159/dart_sdk.js:59904:30)
at http://localhost:44159/:70:11123

Is there a way?

Thanks.

Convocation answered 2/9, 2020 at 11:27 Comment(0)
M
2

try to do like this:

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

if (kIsWeb) {
  //do something
}
Majka answered 2/9, 2020 at 13:43 Comment(1)
How does this answer the question? OP is not trying to find if it's on web but in which OS it is.Friesian
P
0
import 'package:flutter/foundation.dart';

if (defaultTargetPlatform == TargetPlatform.android) {
  //current platform is Android
} else if(defaultTargetPlatform == TargetPlatform.macOS){
  //current platform is macOS
}...
Payola answered 29/6, 2021 at 12:13 Comment(0)
P
0
import 'package:flutter/foundation.dart' show kIsWeb;
    
if (kIsWeb) {
    // https://developer.mozilla.org/en-US/docs/Web/API/Navigator/userAgentData
}

For the next person that has the same question as Antonio/OP. (I'd comment but not enough points)

Prudence answered 29/1, 2023 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.