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

Bloc for resuming uploads to Firebase Storage after app restarts

This package provides a bloc for managing uploads to Firebase Storage. On all non-web platforms, files are temporarily saved to local storage and then uploaded. If files aren't able to finish uploading before the app closes, any remaining files saved to local storage will automatically be retried when the bloc is created during app startup.

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.