Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用Flatter Web将文件上载到Firebase存储';s图像选择器_Firebase_Flutter_Firebase Storage_Flutter Web - Fatal编程技术网

无法使用Flatter Web将文件上载到Firebase存储';s图像选择器

无法使用Flatter Web将文件上载到Firebase存储';s图像选择器,firebase,flutter,firebase-storage,flutter-web,Firebase,Flutter,Firebase Storage,Flutter Web,我用的是颤振网。我正在尝试使用image_picker上传图像并存储在firebase存储中。图像选择器返回PickedFile类型。所以,我使用File image=File(pickedFile.path)将其转换为文件类型,然后使用ref.putFile(image)上传。但是文件没有上传。我得到一个命名空间异常。有什么想法吗 PickedFile pickedFile = await picker.getImage(source: ImageSource.galler

我用的是颤振网。我正在尝试使用image_picker上传图像并存储在firebase存储中。图像选择器返回PickedFile类型。所以,我使用File image=File(pickedFile.path)将其转换为文件类型,然后使用ref.putFile(image)上传。但是文件没有上传。我得到一个命名空间异常。有什么想法吗

PickedFile pickedFile =
          await picker.getImage(source: ImageSource.gallery);

      File newFile = File(pickedFile.path);
      var now = DateTime.now().millisecondsSinceEpoch;
      StorageReference reference =
          FirebaseStorage.instance.ref().child("images/$now");
      StorageUploadTask uploadTask = reference.putFile(newFile);
      //Upload the file to firebase

      StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;

      // Waits till the file is uploaded then stores the download url
      String url = await taskSnapshot.ref.getDownloadURL();
我得到的错误是

Error: Unsupported operation: _Namespace
    at Object.throw_ [as throw] (http://localhost:64148/dart_sdk.js:4322:11)
    at Function.get _namespace [as _namespace] (http://localhost:64148/dart_sdk.js:54027:17)
    at io._File.new.existsSync (http://localhost:64148/dart_sdk.js:51618:51)
    at firebase_storage.StorageReference.__.putFile (http://localhost:64148/packages/firebase_storage/firebase_storage.dart.lib.js:701:27)
    at add_product$46view._AddProductPageState.new.loadAssets (http://localhost:64148/packages/ecommerce_glasses/product/views/add_product.view.dart.lib.js:1234:38)
    at loadAssets.next (<anonymous>)
    at http://localhost:64148/dart_sdk.js:37211:33
    at _RootZone.runUnary (http://localhost:64148/dart_sdk.js:37065:58)
    at _FutureListener.thenAwait.handleValue (http://localhost:64148/dart_sdk.js:32049:29)
    at handleValueCallback (http://localhost:64148/dart_sdk.js:32596:49)
    at Function._propagateToListeners (http://localhost:64148/dart_sdk.js:32634:17)
    at _Future.new.[_completeWithValue] (http://localhost:64148/dart_sdk.js:32477:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:64148/dart_sdk.js:32499:35)
    at Object._microtaskLoop (http://localhost:64148/dart_sdk.js:37326:13)
    at _startMicrotaskLoop (http://localhost:64148/dart_sdk.js:37332:13)
    at http://localhost:64148/dart_sdk.js:32851:9
错误:不支持的操作:\u命名空间
at Object.throw uu[as throw](http://localhost:64148/dart_sdk.js:4322:11)
在Function.get_namespace[作为_namespace](http://localhost:64148/dart_sdk.js:54027:17)
在io.\u File.new.existsSync(http://localhost:64148/dart_sdk.js:51618:51)
在firebase_storage.StorageReference._uuu.putFile(http://localhost:64148/packages/firebase_storage/firebase_storage.dart.lib.js:701:27)
在add_product$46view.\u AddProductPageState.new.loadAssets(http://localhost:64148/packages/ecommerce_glasses/product/views/add_product.view.dart.lib.js:1234:38)
在loadAssets.next()上
在http://localhost:64148/dart_sdk.js:37211:33
at_RootZone.runUnary(http://localhost:64148/dart_sdk.js:37065:58)
在未来的聆听者。然后等待。handleValue(http://localhost:64148/dart_sdk.js:32049:29)
在handleValueCallback(http://localhost:64148/dart_sdk.js:32596:49)
at函数。\u传播到侦听器(http://localhost:64148/dart_sdk.js:32634:17)
在_Future.new.[u completeWithValue](http://localhost:64148/dart_sdk.js:32477:23)
异步时。\u AsyncCallbackEntry.new.callback(http://localhost:64148/dart_sdk.js:32499:35)
在对象上。\u微任务循环(http://localhost:64148/dart_sdk.js:37326:13)
at_startMicrotaskLoop(http://localhost:64148/dart_sdk.js:37332:13)
在http://localhost:64148/dart_sdk.js:32851:9

在index.html中添加此脚本

<script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-app.js"></script>    
<script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-storage.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-firestore.js"></script>

我就是这样做的

// ignore: avoid_web_libraries_in_flutter
import 'dart:html' as html;

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase/firebase.dart' as fb;

Future<Uri> _uploadImageFile({
  @required html.File image,
  @required String imageName,
}) async {
fb.StorageReference storageRef = fb.storage().ref('category/$imageName');
fb.UploadTaskSnapshot uploadTaskSnapshot =
    await storageRef.put(image).future;

 Uri imageUri = await uploadTaskSnapshot.ref.getDownloadURL();
 return imageUri;
}
//忽略:避免web库在flatter中
将“dart:html”导入为html;
导入“包:cloud_firestore/cloud_firestore.dart”;
将“包:firebase/firebase.dart”作为fb导入;
未来上传图像文件({
@所需的html.File图像,
@必需的字符串imageName,
})异步的{
fb.StorageReference-storageRef=fb.storage().ref('category/$imageName');
fb.UploadTaskSnapshot UploadTaskSnapshot=
等待storageRef.put(图像)。未来;
Uri imageUri=Wait uploadTaskSnapshot.ref.getDownloadURL();
返回imageUri;
}

您不能使用
文件newFile=File(pickedFile.path)在颤振web中。我不知道Firebase API是如何工作的,但您可能必须使用
pickedFile.readAsBytes()

您无法访问Flatter web中的文件路径

您需要以字节形式上载文件,而不是尝试从路径上载文件

您可以使用以下命令以
Uint8List
的形式获取文件:

final fileBytes = pickedFile.readAsBytes();
在你的存储代码中,你可以把数据放进去

reference.putData(fileBytes);
因此,您的代码应该如下所示


PickedFile PickedFile=
等待picker.getImage(来源:ImageSource.gallery);
final fileBytes=pickedFile.readAsBytes();
var now=DateTime.now().millissecondssinceepoch;
存储参考=
FirebaseStorage.instance.ref().child(“images/$now”);
StorageUploadTask uploadTask=reference.putData(fileBytes);
//将文件上载到firebase
StorageTaskSnapshot taskSnapshot=等待上载Task.onComplete;
//等待文件上载,然后存储下载url
字符串url=await taskSnapshot.ref.getDownloadURL();

im使用此软件包映像\u picker\u web:^1.0.9