Skip to content

Commit 49e1beb

Browse files
authored
feat(ui): add firebase_ui_database (#9341)
1 parent f3e4e99 commit 49e1beb

File tree

141 files changed

+6507
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+6507
-0
lines changed
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
migrate_working_dir/
12+
13+
# IntelliJ related
14+
*.iml
15+
*.ipr
16+
*.iws
17+
.idea/
18+
19+
# The .vscode folder contains launch configuration and tasks you configure in
20+
# VS Code which you may wish to be included in version control, so this line
21+
# is commented out by default.
22+
#.vscode/
23+
24+
# Flutter/Dart/Pub related
25+
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
26+
/pubspec.lock
27+
**/doc/api/
28+
.dart_tool/
29+
.packages
30+
build/
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: f1875d570e39de09040c8f79aa13cc56baab8db1
8+
channel: stable
9+
10+
project_type: package
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## 1.0.0-dev.0
2+
3+
- Bump "firebase_ui_database" to `1.0.0-dev.0`.
4+
5+
## 0.0.1
6+
7+
* TODO: Describe initial release.

packages/firebase_ui_database/LICENSE

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Copyright 2017, the Chromium project authors. All rights reserved.
2+
Redistribution and use in source and binary forms, with or without
3+
modification, are permitted provided that the following conditions are
4+
met:
5+
6+
* Redistributions of source code must retain the above copyright
7+
notice, this list of conditions and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above
9+
copyright notice, this list of conditions and the following
10+
disclaimer in the documentation and/or other materials provided
11+
with the distribution.
12+
* Neither the name of Google Inc. nor the names of its
13+
contributors may be used to endorse or promote products derived
14+
from this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include: package:flutter_lints/flutter.yaml
2+
3+
# Additional information about this file can be found at
4+
# https://dart.dev/guides/language/analysis-options
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
migrate_working_dir/
12+
13+
# IntelliJ related
14+
*.iml
15+
*.ipr
16+
*.iws
17+
.idea/
18+
19+
# The .vscode folder contains launch configuration and tasks you configure in
20+
# VS Code which you may wish to be included in version control, so this line
21+
# is commented out by default.
22+
#.vscode/
23+
24+
# Flutter/Dart/Pub related
25+
**/doc/api/
26+
**/ios/Flutter/.last_build_id
27+
.dart_tool/
28+
.flutter-plugins
29+
.flutter-plugins-dependencies
30+
.packages
31+
.pub-cache/
32+
.pub/
33+
/build/
34+
35+
# Web related
36+
lib/generated_plugin_registrant.dart
37+
38+
# Symbolication related
39+
app.*.symbols
40+
41+
# Obfuscation related
42+
app.*.map.json
43+
44+
# Android Studio will place build artifacts here
45+
/android/app/debug
46+
/android/app/profile
47+
/android/app/release
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled.
5+
6+
version:
7+
revision: f1875d570e39de09040c8f79aa13cc56baab8db1
8+
channel: stable
9+
10+
project_type: app
11+
12+
# Tracks metadata for the flutter migrate command
13+
migration:
14+
platforms:
15+
- platform: root
16+
create_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
17+
base_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
18+
- platform: android
19+
create_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
20+
base_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
21+
- platform: ios
22+
create_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
23+
base_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
24+
- platform: linux
25+
create_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
26+
base_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
27+
- platform: macos
28+
create_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
29+
base_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
30+
- platform: web
31+
create_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
32+
base_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
33+
- platform: windows
34+
create_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
35+
base_revision: f1875d570e39de09040c8f79aa13cc56baab8db1
36+
37+
# User provided section
38+
39+
# List of Local paths (relative to this file) that should be
40+
# ignored by the migrate tool.
41+
#
42+
# Files that are not part of the templates will be ignored by default.
43+
unmanaged_files:
44+
- 'lib/main.dart'
45+
- 'ios/Runner.xcodeproj/project.pbxproj'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# firebase_ui_database_example
2+
3+
A new Flutter project.
4+
5+
## Getting Started
6+
7+
This project is a starting point for a Flutter application.
8+
9+
A few resources to get you started if this is your first Flutter project:
10+
11+
- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
12+
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)
13+
14+
For help getting started with Flutter development, view the
15+
[online documentation](https://docs.flutter.dev/), which offers tutorials,
16+
samples, guidance on mobile development, and a full API reference.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This file configures the analyzer, which statically analyzes Dart code to
2+
# check for errors, warnings, and lints.
3+
#
4+
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
5+
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
6+
# invoked from the command line by running `flutter analyze`.
7+
8+
# The following line activates a set of recommended lints for Flutter apps,
9+
# packages, and plugins designed to encourage good coding practices.
10+
include: package:flutter_lints/flutter.yaml
11+
12+
linter:
13+
# The lint rules applied to this project can be customized in the
14+
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
15+
# included above or to enable additional rules. A list of all available lints
16+
# and their documentation is published at
17+
# https://dart-lang.github.io/linter/lints/index.html.
18+
#
19+
# Instead of disabling a lint rule for the entire project in the
20+
# section below, it can also be suppressed for a single line of code
21+
# or a specific dart file by using the `// ignore: name_of_lint` and
22+
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
23+
# producing the lint.
24+
rules:
25+
# avoid_print: false # Uncomment to disable the `avoid_print` rule
26+
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
27+
28+
# Additional information about this file can be found at
29+
# https://dart.dev/guides/language/analysis-options

0 commit comments

Comments
 (0)