在 Firebase 存储 flutter dart 中上传图片
Uploading picture in Firebase storage flutter dart
我正在尝试从我的画廊上传图片,代码工作正常并且 运行ning,但我的设备上没有。我想知道为什么它对我不起作用请帮忙
这是我尝试 运行.
时显示的内容
图片选择也正常
如果 Firebase 但图像在存储中不可见,则名称和标题以及显示的所有内容。
Future selectFile() async {
final result = await FilePicker.platform
.pickFiles(allowMultiple: false, type: FileType.any);
if (result != null) {
final path = result.files.single.path;
file = File(path!);
setState(() => file = File(path));
}
}
Future uploadFile() async {
if (file == null) return;
final fileName = basename(file!.path);
final destination = 'Content/$fileName';
task = FirebaseApi.uploadFile(destination, file!);
setState(() {});
}
Widget buildUploadStatus(UploadTask uploadTask) =>
StreamBuilder<TaskSnapshot>(
stream: task!.snapshotEvents,
builder: (context, snapshot) {
if (snapshot.hasData) {
final snap = snapshot.data!;
final progress = snap.bytesTransferred / snap.totalBytes;
final percentage = (progress * 100).toStringAsFixed(2);
return Row(
children: [
Text(
'$percentage %',
style: GoogleFonts.asap(
fontSize: 17,
color: Colors.white,
fontWeight: FontWeight.w500,
),
)
],
);
} else {
return Container();
}
},
);
@override
State<StatefulWidget> createState() {
// TODO: implement createState
throw UnimplementedError();
}
}
class StatfulWidget {}
class FirebaseApi {
static UploadTask? uploadFile(String destination, File file) {
try {
final ref = FirebaseStorage.instance.ref(destination);
return ref.putFile(file);
} on FirebaseException catch (e) {
return null;
}
}
}
那是因为您为数据库提供了一个空值。我遇到了一个类似的问题,我将最新的消息发送到数据库并用“。”初始化它。相反,使用类似 space " ".
的东西
示例代码:
admin.firestore().collection('whatever').doc(""+id);
或者使用用反引号包围的 ${id} `
另一种解决方案是数据需要采用 JSON 格式。默认情况下,它设置为文本,因此必须将其更改为 JSON (application/json).
这是与同一问题相关的 Github 论坛:https://github.com/firebase/firebase-admin-node/issues/320
我正在尝试从我的画廊上传图片,代码工作正常并且 运行ning,但我的设备上没有。我想知道为什么它对我不起作用请帮忙 这是我尝试 运行.
时显示的内容图片选择也正常 如果 Firebase 但图像在存储中不可见,则名称和标题以及显示的所有内容。
Future selectFile() async {
final result = await FilePicker.platform
.pickFiles(allowMultiple: false, type: FileType.any);
if (result != null) {
final path = result.files.single.path;
file = File(path!);
setState(() => file = File(path));
}
}
Future uploadFile() async {
if (file == null) return;
final fileName = basename(file!.path);
final destination = 'Content/$fileName';
task = FirebaseApi.uploadFile(destination, file!);
setState(() {});
}
Widget buildUploadStatus(UploadTask uploadTask) =>
StreamBuilder<TaskSnapshot>(
stream: task!.snapshotEvents,
builder: (context, snapshot) {
if (snapshot.hasData) {
final snap = snapshot.data!;
final progress = snap.bytesTransferred / snap.totalBytes;
final percentage = (progress * 100).toStringAsFixed(2);
return Row(
children: [
Text(
'$percentage %',
style: GoogleFonts.asap(
fontSize: 17,
color: Colors.white,
fontWeight: FontWeight.w500,
),
)
],
);
} else {
return Container();
}
},
);
@override
State<StatefulWidget> createState() {
// TODO: implement createState
throw UnimplementedError();
}
}
class StatfulWidget {}
class FirebaseApi {
static UploadTask? uploadFile(String destination, File file) {
try {
final ref = FirebaseStorage.instance.ref(destination);
return ref.putFile(file);
} on FirebaseException catch (e) {
return null;
}
}
}
那是因为您为数据库提供了一个空值。我遇到了一个类似的问题,我将最新的消息发送到数据库并用“。”初始化它。相反,使用类似 space " ".
的东西示例代码: admin.firestore().collection('whatever').doc(""+id); 或者使用用反引号包围的 ${id} `
另一种解决方案是数据需要采用 JSON 格式。默认情况下,它设置为文本,因此必须将其更改为 JSON (application/json).
这是与同一问题相关的 Github 论坛:https://github.com/firebase/firebase-admin-node/issues/320