gpt4 book ai didi

firebase - FirebaseStorage抖动

转载 作者:行者123 更新时间:2023-12-03 04:25:53 25 4
gpt4 key购买 nike

我是Firebase的新手,我无法弄清楚我应该将什么样的参数传递给putfile中的
以下代码,是否有任何有关flutter的Firebase的教程或适当的文档?

实现代码

import "dart:io";
import "package:flutter/material.dart";
import "package:firebase_storage/firebase_storage.dart";
import "package:flutter/widgets.dart";

class uploadApp extends StatefulWidget{
@override
State<StatefulWidget> createState() =>_uploadApp();
}

class _uploadApp extends State<uploadApp>{

var count=1;
final File file = File("images/ts2.jpg");
final FirebaseStorage _storage = FirebaseStorage(storageBucket:"gs://console.firebase.google.com/project/baby-name-4ef54/storage/baby-name-4ef54.appspot.com");
StorageUploadTask _uploadTask;

void firebasebackend(){
String filePath = "images/$count.jpg";
setState(() {
_uploadTask = _storage.ref().child(filePath).putFile(file);
});
}

@override
Widget build(BuildContext context) {
return MaterialApp(
title:"upload",
home:Material(
child:Container(child:Center(
child:
Column(
children: <Widget>[
Container(
width: 300.0,
height: 300.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(300.0)),
image:DecorationImage(
image: AssetImage("images/ts2.jpg"),
fit: BoxFit.cover
)
),
),
Center(
child: RaisedButton(
child:Text("Upload"),
onPressed: (){
firebasebackend();
setState(){
count=count+1;
};
},
),
)
],
),
)
)
)
);
}
}

'package:firebase_storage/src/storage_reference.dart': Failed assertion: line 62 pos 12: 'file.existsSync()': is not true.



将图片直接从文件夹直接上传到Firebase存储,该怎么办?

最佳答案

根据firebase_storage插件的代码:

  /// Asynchronously uploads a file to the currently specified
/// [StorageReference], with an optional [metadata].
StorageUploadTask putFile(File file, [StorageMetadata metadata]) {
assert(file.existsSync());
final _StorageFileUploadTask task =
_StorageFileUploadTask._(file, _firebaseStorage, this, metadata);
task._start();
return task;
}

file作为参数,并将类型 StorageMetadata的变量作为 可选参数。

https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_storage/lib/src/storage_reference.dart#L61

https://api.dart.dev/stable/2.7.0/dart-io/File-class.html

关于firebase - FirebaseStorage抖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59811157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com