|
| 1 | +## Firebase UI Storage |
| 2 | + |
| 3 | +[](https://pub.dev/packages/firebase_ui_storage) |
| 4 | + |
| 5 | +Firebase UI Storage is a set of Flutter widgets and utilities designed to help you build and integrate your user interface with Firebase Storage. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +Intall dependencies |
| 10 | + |
| 11 | +```sh |
| 12 | +flutter pub add firebase_core firebase_storage firebase_ui_storage |
| 13 | +``` |
| 14 | + |
| 15 | +Donwload Firebase project config |
| 16 | + |
| 17 | +```sh |
| 18 | +flutterfire configure |
| 19 | +``` |
| 20 | + |
| 21 | +## Configuration |
| 22 | + |
| 23 | +This section will walk you through the configuration process of the Firebase UI Storage |
| 24 | + |
| 25 | +### macOS |
| 26 | + |
| 27 | +If you're building for macOS, you will need to add an entitlement for either read-only access if you only upload files: |
| 28 | + |
| 29 | +```xml |
| 30 | + <key>com.apple.security.files.user-selected.read-only</key> |
| 31 | + <true/> |
| 32 | +``` |
| 33 | + |
| 34 | +or read/write access if you want to be able to download files as well: |
| 35 | + |
| 36 | +```xml |
| 37 | + <key>com.apple.security.files.user-selected.read-write</key> |
| 38 | + <true/> |
| 39 | +``` |
| 40 | + |
| 41 | +Make sure to add network client entitlement as well: |
| 42 | + |
| 43 | +```xml |
| 44 | +<key>com.apple.security.network.client</key> |
| 45 | +<true/> |
| 46 | +``` |
| 47 | + |
| 48 | +### FirebaseUIStorage.configure() |
| 49 | + |
| 50 | +To reduce boilerplate for widgets, `FirebaseUIStroage` has a top-level configuration: |
| 51 | + |
| 52 | +```dart |
| 53 | +import 'package:firebase_core/firebase_core.dart'; |
| 54 | +import 'package:firebase_storage/firebase_storage.dart'; |
| 55 | +import 'package:firebase_ui_storage/firebase_ui_storage.dart'; |
| 56 | +import 'package:flutter/material.dart'; |
| 57 | +
|
| 58 | +import 'firebase_options.dart'; |
| 59 | +
|
| 60 | +Future<void> main() async { |
| 61 | + WidgetsFlutterBinding.ensureInitialized(); |
| 62 | +
|
| 63 | + await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); |
| 64 | +
|
| 65 | + final storage = FirebaseStorage.instance; |
| 66 | + final config = FirebaseUIStorageConfiguration(storage: storage); |
| 67 | +
|
| 68 | + await FirebaseUIStorage.configure(config); |
| 69 | +
|
| 70 | + runApp(const MyApp()); |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +See [API docs](https://pub.dev/documentation/firebase_ui_storage/latest/firebase_ui_storage/FirebaseUIStorageConfiguration-class.html) for more configuration options. |
| 75 | + |
| 76 | +### Overriding configuration |
| 77 | + |
| 78 | +It is possible to override a top-level configuration for a widget subtree: |
| 79 | + |
| 80 | +```dart |
| 81 | +class MyWidget extends StatelessWidget { |
| 82 | + @override |
| 83 | + Widget build(BuildContext context) { |
| 84 | + return FirebaseUIStorageConfigOverride( |
| 85 | + config: FirebaseUIStorageConfiguration( |
| 86 | + uploadRoot: storage.ref('${FirebaseAuth.instance.currentUser.uid}/'), |
| 87 | + namingPolicy: const UuidFileUploadNamingPolicy(), |
| 88 | + child: const MyUserPage(), |
| 89 | + ), |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +## Widgets |
| 96 | + |
| 97 | +### UploadButton |
| 98 | + |
| 99 | +```dart |
| 100 | +class MyUploadPage extends StatelessWidget { |
| 101 | + @override |
| 102 | + Widget build(BuildContext context) { |
| 103 | + return Scaffold( |
| 104 | + body: Center( |
| 105 | + child: UploadButton( |
| 106 | + mimeTypes: const ['image/png', 'image/jpeg'], |
| 107 | + onError: (err, stackTrace) { |
| 108 | + print(err.toString()); |
| 109 | + }, |
| 110 | + onUploadComplete: (ref) { |
| 111 | + print('File uploaded to ${ref.path}'); |
| 112 | + } |
| 113 | + ), |
| 114 | + ), |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
0 commit comments