Wrappers and tools for composing Firebase Cloud Functions in the Dart language
π₯ | No interop |
π§ | Limited interop |
π¨ | Partial interop |
π© | Full interop |
Service | Status | Comment |
---|---|---|
App | π© | |
App Check | π₯ | App Check enforcement for callable functions is supported |
Auth | π₯ | |
Credential | π₯ | |
Database | π₯ | |
Eventarc | π₯ | |
Extensions | π₯ | |
Firestore | π© | |
Functions | π₯ | |
Installations | π₯ | |
Messaging | π§ | Only message sending |
Project Management | π₯ | |
Remote Config | π₯ | |
Security Rules | π₯ | |
Storage | π₯ |
Service | Status |
---|---|
Alerts | π₯ |
Database | π₯ |
Eventarc | π₯ |
Firestore | π© |
HTTPS | π© |
Identity | π© |
PubSub | π₯ |
Remote Config | π₯ |
Scheduler | π₯ |
Storage | π₯ |
Tasks | π₯ |
Test Lab | π₯ |
Global Options | π₯ |
onInit | π₯ |
Interop with the following packages is planned, but not yet implemented:
Install the firebase tools
Create the following dart/flutter packages:
Package | Purpose |
---|---|
{app} | Flutter app |
{app}_core | Base core package |
{app}_core_flutter | Core package for Flutter app |
{app}_core_js_interop | Core package for Cloud Functions |
{app}_firebase/functions | Firebase Cloud Functions |
The purpose of the three core packages is as follows:
- Flutter apps (besides the web platform) cannot import js_interop code
- Transpiled dart2js code cannot transitively depend on Flutter
This means the base {app}_core
package cannot depend on anything that depends on Flutter. Those dependencies should be used in {app}_core_flutter
instead.
Run firebase init
in {app}_firebase
. Choose javascript as the language for Cloud Functions.
Make the following modifications to files in {app}_firebase
:
Add the following ignores
{
"functions": [
{
"ignore": [
"pubspec.*",
"src",
"tool",
"analysis_options.yaml",
".dart_tool"
]
}
]
}
# Compiled JavaScript files
lib/**
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
{
"scripts": {
"lint": "dart analyze --fatal-infos",
"build": "dart run tool/build.dart"
},
"main": "lib/index.js"
}
dependencies:
{app}_core_js_interop:
path: ../../{app}_core_js_interop
firebase_js_interop: latest
This is where the Dart Cloud Functions code lives. See below for an example.
This is what compiles index.dart
into an index.js
file that can run on Cloud Functions
import 'package:firebase_js_interop/build.dart';
void main() async {
await buildCloudFunctions();
}
Create the base models in {app}_core
, then augment them with json serialization in {app}_core_flutter
and {app}_core_js_interop
. See the example project for more details on how to use json serialization with both firebase_js_interop
and cloud_firestore_odm
.
Writing Cloud Functions in Dart is very similar to writing them in TypeScript:
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'package:firebase_js_interop/functions.dart';
import 'package:firebase_js_interop/node.dart';
import 'package:firebase_js_interop/express.dart' as express;
void main() {
exports['helloWorld'] = FirebaseFunctions.https.onRequest(
(Request request, express.Response response) {
return response.send('Hello from Firebase!'.toJS);
}.toJS,
);
}
Make sure to return promises for async operations:
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'package:firebase_js_interop/functions.dart';
import 'package:firebase_js_interop/js.dart';
import 'package:firebase_js_interop/node.dart';
import 'package:firebase_js_interop/admin.dart';
void main() {
exports['onMessageCreated'] = FirebaseFunctions.firestore.onDocumentCreated(
'/chats/{chatId}/messages/{messageId}'.toJS,
(FirestoreEvent<DocumentSnapshot> event) {
return promise(() async {
await Future.delayed(const Duration(seconds: 1));
});
}.toJS,
);
}
dart run tool/build.dart
firebase deploy --only functions
The compiled JS will also work in the Firebase emulators
See firebase_rules for writing Firebase security rules in Dart.
There are many unimplemented features. Please feel free to contribute if any necessary components are missing.
See CONTRIBUTING.md for details