Skip to content

Instantly share code, notes, and snippets.

@iamEtornam
Last active January 29, 2022 11:24
Show Gist options
  • Save iamEtornam/ebb28cd1b07f3155e0371b43a5010a19 to your computer and use it in GitHub Desktop.
Save iamEtornam/ebb28cd1b07f3155e0371b43a5010a19 to your computer and use it in GitHub Desktop.
A simple code snippet to upload any file type to firebase storage
```dart
import 'package:mime/mime.dart'; //add mime to pubspec.yaml
import 'package:uuid/uuid.dart'; //add uuid to pubspec.yaml
import 'package:firebase_storage/firebase_storage.dart'; //add firebase storage to pubspec.yaml
import 'package:path/path.dart'; //add path to pubspec.yaml
final firebaseStorage = FirebaseStorage.instance;
Future uploadFile({required File file}) async {
const uuid = Uuid();
final String fileName = basename(file.path);
final List<String> mimes = lookupMimeType(fileName)!.split('/');
final String newFileName = '${uuid.v4()}.${mimes.last}'; //filename with extension
final UploadTask uploadTask = firebaseStorage.ref().child('reports').child(newFileName).putFile(file);
final TaskSnapshot downloadUri = await Future.value(uploadTask);
final String downloadUrl = (await downloadUri.ref.getDownloadURL());
return downloadUrl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment