【问题标题】:Flutter web platform checkFlutter Web 平台检查
【发布时间】:2020-12-21 13:33:38
【问题描述】:

我正在尝试找到一种方法来查看我的 Flutter Web 项目在哪个平台(iOS 或 Android)上运行。当我使用if (Platform.isIOS) { //do something } 时,我遇到以下错误:

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

有办法吗?

谢谢。

【问题讨论】:

    标签: flutter dart flutter-web


    【解决方案1】:

    尝试这样做:

    import 'package:flutter/foundation.dart' show kIsWeb;
    
    if (kIsWeb) {
      //do something
    }
    

    【讨论】:

      【解决方案2】:
      import 'package:flutter/foundation.dart';
      
      if (defaultTargetPlatform == TargetPlatform.android) {
        //current platform is Android
      } else if(defaultTargetPlatform == TargetPlatform.macOS){
        //current platform is macOS
      }...
      

      【讨论】:

        猜你喜欢
        • 2020-09-24
        • 1970-01-01
        • 1970-01-01
        • 2021-05-29
        • 2020-08-23
        • 2012-03-13
        • 1970-01-01
        • 1970-01-01
        • 2020-07-21
        相关资源
        最近更新 更多