Flutter Web 给出有关“不支持的操作”的错误:Platform._operatingSystem

发布于 2025-01-10 01:22:09 字数 5522 浏览 1 评论 0原文

Flutter web 给出有关 Unsupported operation: Platform._operatingSystem 代码在 flutter android 上正常运行的错误。我认为错误在于我的上传图像方法,它在 flutter web 上不起作用。

Here is the error

    Error: Unsupported operation: Platform._operatingSystem
    at Object.throw_ [as throw] (http://localhost:37805/dart_sdk.js:5388:11)
    at Function._operatingSystem (http://localhost:37805/dart_sdk.js:61565:17)
    at Function.get operatingSystem [as operatingSystem] (http://localhost:37805/dart_sdk.js:61611:27)
    at get _operatingSystem (http://localhost:37805/dart_sdk.js:61524:27)
    at Function.desc.get [as _operatingSystem] (http://localhost:37805/dart_sdk.js:5897:17)
    at get isWindows (http://localhost:37805/dart_sdk.js:61542:26)
    at Function.desc.get [as isWindows] (http://localhost:37805/dart_sdk.js:5897:17)
    at Function._isAbsolute (http://localhost:37805/dart_sdk.js:56712:33)
    at io._File.new.get isAbsolute [as isAbsolute] (http://localhost:37805/dart_sdk.js:56708:34)
    at io._File.new.get [_absolutePath] (http://localhost:37805/dart_sdk.js:56719:26)
    at io._File.new.get absolute [as absolute] (http://localhost:37805/dart_sdk.js:58252:30)
    at firebase_storage.Reference.__.putFile (http://localhost:37805/packages/firebase_storage/firebase_storage.dart.lib.js:385:27)
    at productprovider.ProductProvider.new.uploadProductImage (http://localhost:37805/packages/phataphat_web_dashboard/services/productprovider.dart.lib.js:223:93)
    at uploadProductImage.next (<anonymous>)
    at runBody (http://localhost:37805/dart_sdk.js:43060:34)
    at Object._async [as async] (http://localhost:37805/dart_sdk.js:43091:7)
    at productprovider.ProductProvider.new.uploadProductImage (http://localhost:37805/packages/phataphat_web_dashboard/services/productprovider.dart.lib.js:218:20)
    at http://localhost:37805/packages/phataphat_web_dashboard/pages/product/widget/addnewproduct.dart.lib.js:1342:47
    at ink_well._InkResponseState.new.[_handleTap] (http://localhost:37805/packages/flutter/src/material/icon_button.dart.lib.js:44059:31)
    at tap.TapGestureRecognizer.new.invokeCallback (http://localhost:37805/packages/flutter/src/gestures/recognizer.dart.lib.js:201:18)
    at tap.TapGestureRecognizer.new.handleTapUp (http://localhost:37805/packages/flutter/src/gestures/tap.dart.lib.js:429:40)
    at tap.TapGestureRecognizer.new.[_checkUp] (http://localhost:37805/packages/flutter/src/gestures/tap.dart.lib.js:229:12)
    at tap.TapGestureRecognizer.new.handlePrimaryPointer (http://localhost:37805/packages/flutter/src/gestures/tap.dart.lib.js:175:23)
    at tap.TapGestureRecognizer.new.handleEvent (http://localhost:37805/packages/flutter/src/gestures/recognizer.dart.lib.js:454:16)
    at pointer_router.PointerRouter.new.[_dispatch] (http://localhost:37805/packages/flutter/src/gestures/pointer_router.dart.lib.js:94:9)
    at http://localhost:37805/packages/flutter/src/gestures/pointer_router.dart.lib.js:125:26
    at LinkedMap.new.forEach (http://localhost:37805/dart_sdk.js:29167:11)
    at pointer_router.PointerRouter.new.[_dispatchEventToRoutes] (http://localhost:37805/packages/flutter/src/gestures/pointer_router.dart.lib.js:122:29)
    at pointer_router.PointerRouter.new.route (http://localhost:37805/packages/flutter/src/gestures/pointer_router.dart.lib.js:114:37)
    at binding$5.WidgetsFlutterBinding.new.handleEvent (http://localhost:37805/packages/flutter/src/gestures/binding.dart.lib.js:397:26)
    at binding$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:37805/packages/flutter/src/gestures/binding.dart.lib.js:383:24)
    at binding$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:37805/packages/flutter/src/rendering/layer.dart.lib.js:5819:13)
    at binding$5.WidgetsFlutterBinding.new.[_handlePointerEventImmediately] (http://localhost:37805/packages/flutter/src/gestures/binding.dart.lib.js:356:14)
    at binding$5.WidgetsFlutterBinding.new.handlePointerEvent (http://localhost:37805/packages/flutter/src/gestures/binding.dart.lib.js:329:43)
    at binding$5.WidgetsFlutterBinding.new.[_flushPointerEventQueue] (http://localhost:37805/packages/flutter/src/gestures/binding.dart.lib.js:318:14)
    at binding$5.WidgetsFlutterBinding.new.[_handlePointerDataPacket] (http://localhost:37805/packages/flutter/src/gestures/binding.dart.lib.js:308:65)
    at Object.invoke1 (http://localhost:37805/dart_sdk.js:204155:7)
    at _engine.EnginePlatformDispatcher.__.invokeOnPointerDataPacket (http://localhost:37805/dart_sdk.js:182962:15)
    at _engine.PointerBinding.__.[_onPointerData] (http://localhost:37805/dart_sdk.js:183925:49)
    at http://localhost:37805/dart_sdk.js:184408:28
    at http://localhost:37805/dart_sdk.js:184361:16
    at loggedHandler (http://localhost:37805/dart_sdk.js:184035:11)

这是代码

 Future<String> uploadProductImage(filePath, productName) async {
    File file = File(filePath);
    var timeStamp = Timestamp.now().millisecondsSinceEpoch;

    FirebaseStorage _storage = FirebaseStorage.instance;

    try {
      await _storage
          .ref('ProductImage/$productName$timeStamp')
          .putFile(file);
    } on FirebaseException catch (e) {
      // e.g, e.code == 'canceled'
      if (kDebugMode) {
        print(e.code);
      }
    }
    String downloadURL = await _storage
        .ref('ProductImage/$productName$timeStamp')
        .getDownloadURL();
    productUrl = downloadURL;
    notifyListeners();
    return downloadURL;
  }

Flutter web is giving an error about Unsupported operation: Platform._operatingSystem codes are running properly on flutter android. I think the error is with my upload image method it's not working on flutter web.

Here is the error

    Error: Unsupported operation: Platform._operatingSystem
    at Object.throw_ [as throw] (http://localhost:37805/dart_sdk.js:5388:11)
    at Function._operatingSystem (http://localhost:37805/dart_sdk.js:61565:17)
    at Function.get operatingSystem [as operatingSystem] (http://localhost:37805/dart_sdk.js:61611:27)
    at get _operatingSystem (http://localhost:37805/dart_sdk.js:61524:27)
    at Function.desc.get [as _operatingSystem] (http://localhost:37805/dart_sdk.js:5897:17)
    at get isWindows (http://localhost:37805/dart_sdk.js:61542:26)
    at Function.desc.get [as isWindows] (http://localhost:37805/dart_sdk.js:5897:17)
    at Function._isAbsolute (http://localhost:37805/dart_sdk.js:56712:33)
    at io._File.new.get isAbsolute [as isAbsolute] (http://localhost:37805/dart_sdk.js:56708:34)
    at io._File.new.get [_absolutePath] (http://localhost:37805/dart_sdk.js:56719:26)
    at io._File.new.get absolute [as absolute] (http://localhost:37805/dart_sdk.js:58252:30)
    at firebase_storage.Reference.__.putFile (http://localhost:37805/packages/firebase_storage/firebase_storage.dart.lib.js:385:27)
    at productprovider.ProductProvider.new.uploadProductImage (http://localhost:37805/packages/phataphat_web_dashboard/services/productprovider.dart.lib.js:223:93)
    at uploadProductImage.next (<anonymous>)
    at runBody (http://localhost:37805/dart_sdk.js:43060:34)
    at Object._async [as async] (http://localhost:37805/dart_sdk.js:43091:7)
    at productprovider.ProductProvider.new.uploadProductImage (http://localhost:37805/packages/phataphat_web_dashboard/services/productprovider.dart.lib.js:218:20)
    at http://localhost:37805/packages/phataphat_web_dashboard/pages/product/widget/addnewproduct.dart.lib.js:1342:47
    at ink_well._InkResponseState.new.[_handleTap] (http://localhost:37805/packages/flutter/src/material/icon_button.dart.lib.js:44059:31)
    at tap.TapGestureRecognizer.new.invokeCallback (http://localhost:37805/packages/flutter/src/gestures/recognizer.dart.lib.js:201:18)
    at tap.TapGestureRecognizer.new.handleTapUp (http://localhost:37805/packages/flutter/src/gestures/tap.dart.lib.js:429:40)
    at tap.TapGestureRecognizer.new.[_checkUp] (http://localhost:37805/packages/flutter/src/gestures/tap.dart.lib.js:229:12)
    at tap.TapGestureRecognizer.new.handlePrimaryPointer (http://localhost:37805/packages/flutter/src/gestures/tap.dart.lib.js:175:23)
    at tap.TapGestureRecognizer.new.handleEvent (http://localhost:37805/packages/flutter/src/gestures/recognizer.dart.lib.js:454:16)
    at pointer_router.PointerRouter.new.[_dispatch] (http://localhost:37805/packages/flutter/src/gestures/pointer_router.dart.lib.js:94:9)
    at http://localhost:37805/packages/flutter/src/gestures/pointer_router.dart.lib.js:125:26
    at LinkedMap.new.forEach (http://localhost:37805/dart_sdk.js:29167:11)
    at pointer_router.PointerRouter.new.[_dispatchEventToRoutes] (http://localhost:37805/packages/flutter/src/gestures/pointer_router.dart.lib.js:122:29)
    at pointer_router.PointerRouter.new.route (http://localhost:37805/packages/flutter/src/gestures/pointer_router.dart.lib.js:114:37)
    at binding$5.WidgetsFlutterBinding.new.handleEvent (http://localhost:37805/packages/flutter/src/gestures/binding.dart.lib.js:397:26)
    at binding$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:37805/packages/flutter/src/gestures/binding.dart.lib.js:383:24)
    at binding$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:37805/packages/flutter/src/rendering/layer.dart.lib.js:5819:13)
    at binding$5.WidgetsFlutterBinding.new.[_handlePointerEventImmediately] (http://localhost:37805/packages/flutter/src/gestures/binding.dart.lib.js:356:14)
    at binding$5.WidgetsFlutterBinding.new.handlePointerEvent (http://localhost:37805/packages/flutter/src/gestures/binding.dart.lib.js:329:43)
    at binding$5.WidgetsFlutterBinding.new.[_flushPointerEventQueue] (http://localhost:37805/packages/flutter/src/gestures/binding.dart.lib.js:318:14)
    at binding$5.WidgetsFlutterBinding.new.[_handlePointerDataPacket] (http://localhost:37805/packages/flutter/src/gestures/binding.dart.lib.js:308:65)
    at Object.invoke1 (http://localhost:37805/dart_sdk.js:204155:7)
    at _engine.EnginePlatformDispatcher.__.invokeOnPointerDataPacket (http://localhost:37805/dart_sdk.js:182962:15)
    at _engine.PointerBinding.__.[_onPointerData] (http://localhost:37805/dart_sdk.js:183925:49)
    at http://localhost:37805/dart_sdk.js:184408:28
    at http://localhost:37805/dart_sdk.js:184361:16
    at loggedHandler (http://localhost:37805/dart_sdk.js:184035:11)

Here are the codes

 Future<String> uploadProductImage(filePath, productName) async {
    File file = File(filePath);
    var timeStamp = Timestamp.now().millisecondsSinceEpoch;

    FirebaseStorage _storage = FirebaseStorage.instance;

    try {
      await _storage
          .ref('ProductImage/$productName$timeStamp')
          .putFile(file);
    } on FirebaseException catch (e) {
      // e.g, e.code == 'canceled'
      if (kDebugMode) {
        print(e.code);
      }
    }
    String downloadURL = await _storage
        .ref('ProductImage/$productName$timeStamp')
        .getDownloadURL();
    productUrl = downloadURL;
    notifyListeners();
    return downloadURL;
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏夜暖风 2025-01-17 01:22:09

正如 Adie 所提到的,该问题已通过使用 Web 版 Image Piker 得到解决。

您可以参考类似的stackoverflow线程

方法一:

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

    如果(kIsWeb){
      //做某事
    }

方法2:

import 'package:flutter/foundation.dart';

if (defaultTargetPlatform == TargetPlatform.android) {
  //当前平台是Android
} else if(defaultTargetPlatform == TargetPlatform.macOS){
  //当前平台是macOS
}...

有关更多信息,您可以参考此 链接,其中讨论了类似的问题。

As mentioned by Adie, the issue has been resolved by using Image Piker for web.

you can refer to the similar stackoverflow thread:

method 1 :

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

    if (kIsWeb) {
      //do something
    }

method 2:

import 'package:flutter/foundation.dart';

if (defaultTargetPlatform == TargetPlatform.android) {
  //current platform is Android
} else if(defaultTargetPlatform == TargetPlatform.macOS){
  //current platform is macOS
}...

For more information you can refer to this link where a similar issue has been discussed.

╰沐子 2025-01-17 01:22:09

我遇到了同样的问题,并通过使用 Platform 库和 kIsWeb 类解决了它:

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

...

// check does not run on the web and it runs on Android OS
if (!kIsWeb && Platform.isAndroid) {
  // run Android codes
}
else {
  // run for other platforms codes
}

I had same problem and solved it by use Platform library and kIsWeb class:

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

...

// check does not run on the web and it runs on Android OS
if (!kIsWeb && Platform.isAndroid) {
  // run Android codes
}
else {
  // run for other platforms codes
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文