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;
}
正如 Adie 所提到的,该问题已通过使用 Image Piker for web 得到解决。
您可以参考类似的stackoverflow线程:
方法一:
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 }...
有关更多信息,您可以参考此链接,其中已经讨论了类似的问题。
我遇到了同样的问题,并通过使用
Platform
库和 kIsWeb
类解决了它:
import 'dart:io';
import 'package:flutter/foundation.dart' show kIsWeb;
...
// check does not run on the web and it runs on Windows OS
if (!kIsWeb && Platform.isWindows) {
// run Windows codes
}
else {
// run for other platforms codes
}