【问题标题】:error: The argument type 'Uint8List?' can't be assigned to the parameter type 'Uint8List'错误:参数类型 \'Uint8List?\' 无法分配给参数类型 \'Uint8List\'
【发布时间】:2022-12-24 23:11:34
【问题描述】:

'参数类型 'Uint8List?'无法分配给参数类型“Uint8List”

我试图将图像从 flutter web 应用程序上传到 firebase 存储,但代码中出现此错误。我该如何解决这个错误。代码如下:

`

``
Future<void> pickImage() async
{
  FilePickerResult? result = await FilePicker.platform.pickFiles();

  if (result != null) {
    Uint8List? fileBytes =result.files.first.bytes;
    String fileName = result.files.first.name;
setState(() {
  fname=fileName;
});
    // Upload file
   await FirebaseStorage.instance.ref('images/$fileName').putData(fileBytes);
  var Downloadurl=await FirebaseStorage.instance.ref('images/$fileName').getDownloadURL();
 setState(() {
   imgUrl=Downloadurl;
 });
  }
}


I tried html input element and image picker package too but cant solve the problem.

【问题讨论】:

    标签: flutter firebase-storage flutter-web


    【解决方案1】:

    如果您确定 fileBytes 不是 null,您可以像这样将它转换为 Uint8List

       await FirebaseStorage.instance.ref('images/$fileName').putData(fileBytes as Uint8List);
    

    【讨论】:

      猜你喜欢
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 2021-07-20
      • 1970-01-01
      • 2021-07-09
      • 2021-08-22
      • 2021-11-03
      相关资源
      最近更新 更多