【发布时间】:2022-10-12 23:44:57
【问题描述】:
我想使用flutter将图像上传到firebase存储中,但是当我没有选择任何图像时会出错。
当我输入将上传图像的代码时,错误开始发生。如果我还删除了空检查,它还会显示一条错误消息,表明它需要进行空检查。
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:project/storage_service.dart';
class UserProfile extends StatefulWidget {
const UserProfile({super.key});
@override
State<UserProfile> createState() => _UserProfileState();
}
class _UserProfileState extends State<UserProfile> {
@override
Widget build(BuildContext context) {
final Storage storage = Storage();
return Scaffold(
appBar: AppBar(
title: Text('Profile Picture'),
),
body: Column(
children: [
Center(
child: ElevatedButton(
onPressed: () async {
final results = await FilePicker.platform.pickFiles(
allowMultiple: false,
type: FileType.custom,
allowedExtensions: ['png', 'jpg'],
);
if (results == null) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('No selected file.')));
}
final path = results?.files.single.path;
final fileName = results?.files.single.name;
// when I added this line of code there will be an error when I did not choose any image
storage
.uploadFile(path!, fileName!)
.then((value) => print('done'));
},
child: Text('Upload profile picture')))
],
),
);
}
}
【问题讨论】:
-
你能提供实际的错误信息吗?
标签: flutter firebase firebase-storage