fire_storage_impl 0.1.1
fire_storage_impl: ^0.1.1 copied to clipboard
A lightweight Firebase Storage implementation for Flutter apps with support for file upload, deletion, toast notifications, and localization.
import 'package:cross_file/cross_file.dart';
import 'package:fire_storage_impl/fire_storage_impl.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(); // Make sure firebase_options.dart is properly configured
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Fire Storage Demo',
theme: ThemeData(primarySwatch: Colors.teal),
home: const UploadDemoPage(),
);
}
}
class UploadDemoPage extends StatefulWidget {
const UploadDemoPage({super.key});
@override
State<UploadDemoPage> createState() => _UploadDemoPageState();
}
class _UploadDemoPageState extends State<UploadDemoPage> {
final FireStorageServiceImpl _storageService = FireStorageServiceImpl();
String? _downloadUrl;
double _uploadProgress = 0.0;
Future<void> _uploadFile() async {
// Example: replace with your own file picker logic
const filePath = '/path/to/your/image.jpg';
final xfile = XFile(filePath);
// Validate XFile without dart:io (works on all platforms)
try {
final len = await xfile.length();
if (len == 0) {
_showMessage('File is empty. Please choose another file.');
return;
}
} catch (_) {
_showMessage('File not found! Please update the path.');
return;
}
final url = await _storageService.uploadFile(
file: xfile,
category: 'images',
collectionPath: 'demo_uploads',
uploadingToastTxt: 'Uploading image...',
metadata: {'demo-key': 'demo-value'},
onProgress: (progress) {
setState(() => _uploadProgress = progress);
},
);
if (url != null) {
setState(() {
_downloadUrl = url;
_uploadProgress = 0.0;
});
} else {
_showMessage('Upload failed.');
}
}
Future<void> _deleteFile() async {
if (_downloadUrl != null) {
final success = await _storageService.deleteFile(
fileUrl: _downloadUrl!,
successTxt: 'Image deleted successfully!',
);
if (success) {
setState(() {
_downloadUrl = null;
_uploadProgress = 0.0;
});
} else {
_showMessage('Failed to delete the file.');
}
}
}
void _showMessage(String msg) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(msg)));
}
@override
Widget build(BuildContext context) {
final progressPercent = (_uploadProgress * 100).toStringAsFixed(0);
return Scaffold(
appBar: AppBar(title: const Text('Fire Storage Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_downloadUrl != null)
Image.network(_downloadUrl!, height: 200)
else
const Text('No image uploaded yet.'),
const SizedBox(height: 20),
if (_uploadProgress > 0 && _uploadProgress < 1)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 32.0),
child: Column(
children: [
LinearProgressIndicator(value: _uploadProgress),
const SizedBox(height: 8),
Text('Uploading: $progressPercent%'),
],
),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _uploadFile,
child: const Text('Upload Local File'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: _deleteFile,
child: const Text('Delete Uploaded File'),
),
],
),
),
);
}
}