0% found this document useful (0 votes)
4 views7 pages

11. FIle Upload to Firebase - Copy

The document provides a guide on using Flutter with Firebase for file operations, including picking, uploading, and deleting files. It outlines the necessary dependencies, code snippets for each operation, and form validation for user input. Additionally, it includes resources for further learning about Flutter and Firebase.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

11. FIle Upload to Firebase - Copy

The document provides a guide on using Flutter with Firebase for file operations, including picking, uploading, and deleting files. It outlines the necessary dependencies, code snippets for each operation, and form validation for user input. Additionally, it includes resources for further learning about Flutter and Firebase.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Flutter

Coding and Programming


11
1 File picker
Step 1: Include the necessary Firebase dependencies in your pubspec.yaml file:

dependencies: file_picker: ^latest_version


Step 2: Use this method
FirebaseFirestore db = FirebaseFirestore.instance;
PlatformFile? pickedFile;
void filePicker() async {
final result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['jpg', 'png'],
);
if (result == null) return;
setState(() {
pickedFile = result.files.first;
});
}
2 Upload file ?
FirebaseStorage storage = FirebaseStorage.instance;
double _progress = 0.0; // To track upload progress
String? _uploadedImageUrl;
Future uploadFile() async {
final path = 'my_images/${pickedFile?.name}';
final file = File(pickedFile!.path!);
try {
Reference ref = storage.ref().child(path);
// Upload the file
UploadTask uploadTask = ref.putFile(file);
// Listen to the upload progress
uploadTask.snapshotEvents.listen((TaskSnapshot snapshot) {
setState(() {
_progress = snapshot.bytesTransferred / snapshot.totalBytes;
});
});
// Get the URL of the uploaded file after completing the upload
TaskSnapshot completedTask = await uploadTask;
String downloadUrl = await completedTask.ref.getDownloadURL();
setState(() {
_uploadedImageUrl = downloadUrl;
});
} catch (e) {
print("============> ${e}");
}
}
2 Delete file ?
FirebaseStorage storage = FirebaseStorage.instance;

Future<void> deleteFile(filePath) async {


try {
Reference ref = storage.refFromURL(filePath);
await ref.delete();
print('File deleted successfully! $filePath');
} catch (e) {
print('Failed to delete file: $e');
}
}
2 Form Validation?
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

void submitForm() {
if (_formKey.currentState!.validate()) {
print("============ >> ok");
}
}

Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [

TextFormField(
controller: _lastNameController,
validator: (value) {
if (value!.isEmpty) {
return "Plase enter the last name";
}
return null;
},
),],),),
Thanks
Do you have any
questions?
!
Resources
 https://flutter.dev
 https://firebase.google.com/docs/firestore

You might also like