|
| 1 | +# Firebase UI for Realtime Database |
| 2 | + |
| 3 | +Firebase UI enables you to easily integrate your application UI with your Realtime database. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```sh |
| 8 | +flutter pub add firebase_database |
| 9 | +flutter pub add firebase_ui_database |
| 10 | +``` |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +Import the Firebase UI for Realtime Database package. |
| 15 | + |
| 16 | +```dart |
| 17 | +import 'package:firebase_ui_database/firebase_ui_database.dart'; |
| 18 | +``` |
| 19 | + |
| 20 | +### Infinite scrolling |
| 21 | + |
| 22 | +Infinite scrolling is the concept of continuously loading more data from a database |
| 23 | +as the user scrolls through your application. This is useful when you have a large |
| 24 | +datasets, as it enables the application to render faster as well as reducing network |
| 25 | +overhead for data the user might never see. |
| 26 | + |
| 27 | +Firebase UI for Realtime Database provides a convenient way to implement infinite scrolling |
| 28 | +using the Realtime Database database with the `FirebaseDatabaseListView` widget. |
| 29 | + |
| 30 | +At a minimum, the widget accepts a Realtime Database query and an item builder. As the user scrolls |
| 31 | +down (or across) your list, more data will be automatically fetched from the database (whilst |
| 32 | +respecting query conditions such as ordering). |
| 33 | + |
| 34 | +To get started, create a query and provide an item builder. For this example, we'll display |
| 35 | +a list of users from the `users` collection: |
| 36 | + |
| 37 | +```dart |
| 38 | +final usersQuery = FirebaseDatabase.instance.ref('users').orderByChild('name'); |
| 39 | +
|
| 40 | +FirebaseDatabaseListView( |
| 41 | + query: usersQuery, |
| 42 | + itemBuilder: (context, snapshot) { |
| 43 | + Map<String, dynamic> user = snapshot.value as Map<String, dynamic>; |
| 44 | +
|
| 45 | + return Text('User name is ${user['name']}'); |
| 46 | + }, |
| 47 | +); |
| 48 | +``` |
| 49 | + |
| 50 | +The `FirebaseDatabaseListView` widget is built on-top of Flutter's own [`ListView`](https://api.flutter.dev/flutter/widgets/ListView-class.html) |
| 51 | +widget, and accepts the same parameters which we can optionally provide. For example, to change the scroll-direction to horizontal: |
| 52 | + |
| 53 | +```dart |
| 54 | +FirebaseDatabaseListView( |
| 55 | + scrollDirection: Axis.horizontal, |
| 56 | + // ... |
| 57 | +); |
| 58 | +``` |
| 59 | + |
| 60 | +### Controlling page size |
| 61 | + |
| 62 | +By default, the widget will fetch 10 items from the collection at a time. This can be changed by providing a `pageSize` parameter: |
| 63 | + |
| 64 | +```dart |
| 65 | +FirebaseDatabaseListView( |
| 66 | + pageSize: 20, |
| 67 | + // ... |
| 68 | +); |
| 69 | +``` |
| 70 | + |
| 71 | +In general, it is good practice to keep this value as small as possible to reduce network overhead. If the height (or width) |
| 72 | +of an individual item is large, it is recommended to lower the page size. |
| 73 | + |
| 74 | +### Loading and error handling |
| 75 | + |
| 76 | +By default, the widget will display a loading indicator while data is being fetched from the database, and ignore any errors which might be thrown |
| 77 | +(such as permission denied). You can override this behavior by providing a `loadingBuilder` and `errorBuilder` parameters to the widget: |
| 78 | + |
| 79 | +```dart |
| 80 | +FirebaseDatabaseListView( |
| 81 | + loadingBuilder: (context) => MyCustomLoadingIndicator(), |
| 82 | + errorBuilder: (context, error, stackTrace) => MyCustomError(error, stackTrace), |
| 83 | + // ... |
| 84 | +); |
| 85 | +``` |
| 86 | + |
| 87 | +### Advanced configuration |
| 88 | + |
| 89 | +In many cases, the `FirebaseDatabaseListView` widget is enough to render simple lists of collection data. |
| 90 | +However, you may have specific requirements which require more control over the widget's behavior |
| 91 | +(such as using a [`GridView`](https://api.flutter.dev/flutter/widgets/GridView-class.html)). |
| 92 | + |
| 93 | +The `FirebaseDatabaseQueryBuilder` provides the building blocks for advanced configuration at the expense of |
| 94 | +requiring more boilerplate code. The widget does not provide any underlying list implementation, instead |
| 95 | +you are expected to provide this yourself. |
| 96 | + |
| 97 | +Much like the `FirebaseDatabaseListView` widget, provide a query and builder: |
| 98 | + |
| 99 | +```dart |
| 100 | +final usersQuery = FirebaseDatabase.instance.ref('users').orderByChild('name'); |
| 101 | +
|
| 102 | +FirebaseDatabaseQueryBuilder( |
| 103 | + query: usersQuery, |
| 104 | + builder: (context, snapshot, _) { |
| 105 | + // ... TODO! |
| 106 | + }, |
| 107 | +); |
| 108 | +``` |
| 109 | + |
| 110 | +The main difference to note here is that the `builder` property returns a `FirebaseQueryBuilderSnapshot`, rather |
| 111 | +than an individual document. The builder returns the current state of the entire query, such as whether |
| 112 | +data is loading, an error has occurred and the documents. |
| 113 | + |
| 114 | +This requires us to implement our own list based implementation. Firstly, let's handle the loading and error |
| 115 | +states: |
| 116 | + |
| 117 | +```dart |
| 118 | +FirebaseDatabaseQueryBuilder( |
| 119 | + query: usersQuery, |
| 120 | + builder: (context, snapshot, _) { |
| 121 | + if (snapshot.isFetching) { |
| 122 | + return const CircularProgressIndicator(); |
| 123 | + } |
| 124 | +
|
| 125 | + if (snapshot.hasError) { |
| 126 | + return Text('Something went wrong! ${snapshot.error}'); |
| 127 | + } |
| 128 | +
|
| 129 | + // ... |
| 130 | + }, |
| 131 | +); |
| 132 | +``` |
| 133 | + |
| 134 | +Next, we now need to return a list-view based implementation for our application to display the data. For example, |
| 135 | +to display a grid of users, we can use the [`GridView`](https://api.flutter.dev/flutter/widgets/GridView-class.html) widget: |
| 136 | + |
| 137 | +```dart |
| 138 | +FirebaseDatabaseQueryBuilder( |
| 139 | + query: usersQuery, |
| 140 | + builder: (context, snapshot, _) { |
| 141 | + // ... |
| 142 | +
|
| 143 | + return GridView.builder( |
| 144 | + itemCount: snapshot.docs.length, |
| 145 | + itemBuilder: (context, index) { |
| 146 | + // if we reached the end of the currently obtained items, we try to |
| 147 | + // obtain more items |
| 148 | + if (snapshot.hasMore && index + 1 == snapshot.docs.length) { |
| 149 | + // Tell FirebaseDatabaseQueryBuilder to try to obtain more items. |
| 150 | + // It is safe to call this function from within the build method. |
| 151 | + snapshot.fetchMore(); |
| 152 | + } |
| 153 | +
|
| 154 | + final user = snapshot.docs[index].value as Map<String, dynamic>; |
| 155 | +
|
| 156 | + return Container( |
| 157 | + padding: const EdgeInsets.all(8), |
| 158 | + color: Colors.teal[100], |
| 159 | + child: const Text("User name is ${user['name']}"), |
| 160 | + ); |
| 161 | + }, |
| 162 | + ); |
| 163 | + }, |
| 164 | +); |
| 165 | +``` |
| 166 | + |
| 167 | +With more power comes more responsibility: |
| 168 | + |
| 169 | +1. Within the `itemBuilder` of our `GridView`, we have to manually ensure that we call the `fetchMore()` method on the snapshot when more data is required. |
| 170 | +1. The `FirebaseDatabaseQueryBuilder` does not provide a list-view based handler, instead you must provide your own implementation. |
0 commit comments