Welcome! This project is a port of Node's Firebase Admin SDK to Dart.
β οΈ This project is still in its early stages, and some features may be missing or bugged. Currently, only Firestore is available, with more to come (auth next).
Before using Firebase, we must first authenticate.
There are currently two options:
service-account.json
fileTo connect using environment variables, you will need to have the Firebase CLI installed.
Once done, you can run:
firebase login
And log-in to the project of your choice.
From there, you can have your Dart program authenticate using the environment with:
import 'package:dart_firebase_admin/dart_firebase_admin.dart';
void main() {
final admin = FirebaseAdminApp.initializeApp(
'<your project name>',
// This will obtain authentication information from the environment
Credential.fromApplicationDefaultCredentials(),
);
// TODO use the Admin SDK
final firestore = Firestore(admin);
firestore.doc('hello/world').get();
}
service-account.json
fileAlternatively, you can choose to use a service-account.json
file.
This file can be obtained in your firebase console by going to:
https://console.firebase.google.com/u/0/project/<your-project-name>/settings/serviceaccounts/adminsdk
Make sure to replace <your-project-name>
with the name of your project.
One there, follow the steps and download the file. Place it anywhere you want in your project.
β οΈ Note: This file should be kept private. Do not commit it on public repositories.
After all of that is done, you can now authenticate in your Dart program using:
import 'package:dart_firebase_admin/dart_firebase_admin.dart';
Future<void> main() async {
final admin = FirebaseAdminApp.initializeApp(
'<your project name>',
// Log-in using the newly downloaded file.
Credential.fromServiceAccount(
File('<path to your service-account.json file>'),
),
);
// TODO use the Admin SDK
final firestore = Firestore(admin);
firestore.doc('hello/world').get();
// Don't forget to close the Admin SDK at the end of your "main"!
await admin.close();
}
First, make sure to follow the steps on how to authenticate.
You should now have an instance of a FirebaseAdminApp
object.
You can now use this object to create a Firestore
object as followed:
// Obtained in the previous steps
FirebaseAdminApp admin;
final firestore = Firestore(admin);
From this point onwards, using Firestore with the admin ADK is roughly equivalent to using FlutterFire.
Using this Firestore
object, you'll find your usual collection/query/document
objects.
For example you can perform a where
query:
// The following lists all users above 18 years old
final collection = firestore.collection('users');
final adults = collection.where('age', WhereFilter.greaterThan, 18);
final adultsSnapshot = await adults.get();
for (final adult in adultsSnapshot.docs) {
print(adult.data()['age']);
}
Composite queries are also supported:
// List users with either John or Jack as first name.
firestore
.collection('users')
.whereFilter(
Filter.or([
Filter.where('firstName', WhereFilter.equal, 'John'),
Filter.where('firstName', WhereFilter.equal, 'Jack'),
]),
);
Alternatively, you can fetch a specific document too:
// Print the age of the user with ID "123"
final user = await firestore.doc('users/123').get();
print(user.data()?['age']);
Firestore | |
---|---|
firestore.listCollections() | β |
reference.id | β |
reference.listCollections() | β |
reference.parent | β |
reference.path | β |
reference.== | β |
reference.withConverter | β |
collection.listDocuments | β |
collection.add | β |
collection.get | β |
collection.create | β |
collection.delete | β |
collection.set | β |
collection.update | β |
collection.collection | β |
query.where('field', operator, value) | β |
query.where('field.path', operator, value) | β |
query.where(FieldPath('...'), operator, value) | β |
query.whereFilter(Filter.and(a, b)) | β |
query.whereFilter(Filter.or(a, b)) | β |
query.startAt | β |
query.startAtDocument | β |
query.startAfter | β |
query.startAfterDocument | β |
query.endAt | β |
query.endAtDocument | β |
query.endAfter | β |
query.endAfterDocument | β |
query.select | β |
query.orderBy | β |
query.limit | β |
query.limitToLast | β |
query.offset | β |
querySnapshot.docs | β |
querySnapshot.readTime | β |
documentSnapshots.data | β |
documentSnapshots.readTime/createTime/updateTime | β |
documentSnapshots.id | β |
documentSnapshots.exists | β |
documentSnapshots.data | β |
documentSnapshots.get(fieldPath) | β |
FieldValue.documentId | β |
FieldValue.increment | β |
FieldValue.arrayUnion | β |
FieldValue.arrayRemove | β |
FieldValue.delete | β |
FieldValue.serverTimestamp | β |
collectionGroup | β |
GeoPoint | β |
Timestamp | β |
querySnapshot.docsChange | β οΈ |
query.onSnapshot | β |
runTransaction | β |
BundleBuilder | β |
First, make sure to follow the steps on how to authenticate.
You should now have an instance of a FirebaseAdminApp
object.
You can now use this object to create a Auth
object as followed:
// Obtained in the previous steps
FirebaseAdminApp admin;
final auth = Auth(admin);
You can then use this Auth
object to perform various
auth operations. For example, you can generate a password reset link:
final link = await auth.generatePasswordResetLink(
'hello@example.com',
);
Auth | |
---|---|
auth.tenantManager | β |
auth.projectConfigManager | β |
auth.generatePasswordResetLink | β |
auth.generateEmailVerificationLink | β |
auth.generateVerifyAndChangeEmailLink | β |
auth.generateSignInWithEmailLink | β |
auth.listProviderConfigs | β |
auth.createProviderConfig | β |
auth.updateProviderConfig | β |
auth.getProviderConfig | β |
auth.deleteProviderConfig | β |
auth.createCustomToken | β |
auth.setCustomUserClaims | β |
auth.verifyIdToken | β |
auth.revokeRefreshTokens | β |
auth.createSessionCookie | β |
auth.verifySessionCookie | β |
auth.importUsers | β |
auth.listUsers | β |
auth.deleteUser | β |
auth.deleteUsers | β |
auth.getUser | β |
auth.getUserByPhoneNumber | β |
auth.getUserByEmail | β |
auth.getUserByProviderUid | β |
auth.getUsers | β |
auth.createUser | β |
auth.updateUser | β |
First, make sure to follow the steps on how to authenticate.
You should now have an instance of a FirebaseAdminApp
object.
Then, you can create an instance of Messaging
as followed:
// Obtained in the previous steps
FirebaseAdminApp admin;
final messaging = Messaging(messaging);
You can then use that Messaging
object to interact with Firebase Messaging.
For example, if you want to send a notification to a specific device, you can do:
await messaging.send(
TokenMessage(
// The token of the targeted device.
// This token can be obtain by using FlutterFire's firebase_messaging:
// https://pub.dev/documentation/firebase_messaging/latest/firebase_messaging/FirebaseMessaging/getToken.html
token: "<targeted device's token>",
notification: Notification(
// The content of the notification
title: 'Hello',
body: 'World',
),
),
);
Messaging | |
---|---|
Messaging.send | β |
Messaging.sendEach | β |
Messaging.sendEachForMulticast | β |
Messaging.subscribeToTopic | β |
Messaging.unsubscribeFromTopic | β |
TokenMessage | β |
TopicMessage | β |
ConditionMessage | β |
Messaging.sendAll | β |
Messaging.sendMulticast | β |
Built and maintained by Invertase.