firebase_storage_upload_bloc 1.0.0 copy "firebase_storage_upload_bloc: ^1.0.0" to clipboard
firebase_storage_upload_bloc: ^1.0.0 copied to clipboard

Bloc for managing uploads to Firebase Storage

Firebase Storage Upload Bloc #

This package provides a simple bloc for managing uploads to Firebase Storage. On all non-web platforms, files are first saved to local storage and then uploaded. Local files are deleted after they are successfully uploaded.

Usage #

  1. Initialize the bloc and expose it using BlocProvider:
runApp(
  BlocProvider<FirebaseStorageUploadBloc>(
    create: (context) => FirebaseStorageUploadBloc(),
  ),
  child: const MyApp(),
);
  1. Call the uploadFile method to upload a file:
final ref = FirebaseStorage.instance.child('test.txt');
final data = 'my test data';
final mimeType = 'text/plain';

context.read<FirebaseStorageUploadBloc>.uploadFile(ref, data, mimeType);
  1. Use the bloc state to show upload progress within the app:
return BlocBuilder<FirebaseStorageUploadBloc, FirebaseStorageUploadState>(
  builder: (context, state) {
    return Text('${state.tasks.length} tasks pending');
  },
);

Each pending upload task is a future of type UploadTask. This means that the app can show detailed progress information for each task. In addition, any future can be added into the bloc. Combined with a progress widget, this makes it easy to show a list of pending tasks (both uploads and other activities) within a single widget. See the example app for details.

Limitations #

This package does not implement background uploads. See the flutter_uploader package for a partial solution which handles uploading files in the background, but that does not support Firebase Storage directly. To upload files to Firebase Storage in the background, flutter_uploader must be combined with a Firebase Cloud Function using resumable uploads. See this discussion for more details.