Dart Functions Framework

Grant Timmerman
Google Cloud - Community
2 min readMar 17, 2021

The Dart Functions Framework allows you to write lightweight serverless functions in Dart that can be deployed to Google Cloud.

In this blogpost, we’ll walk through getting started with the framework and deploy a function to Cloud Run 💨👟.

Install the Dart SDK

You can install Dart for your machine at dart.dev/get-dart.

For macOS, run the following commands:

brew tap dart-lang/dart
brew install dart

Install the Dart Functions Framework, dartfn

The dartfn command-line tool helps with scaffolding functions like helloworld, cloudevent, and json.

Install the Dart Functions Framework CLI (dartfn) with:

dart pub global activate dartfn

Create a “Hello, World” function using the generator:

mkdir helloworld
cd helloworld
dartfn generate helloworld

This will output:

project: helloworld
Creating helloworld application `helloworld`:
helloworld/.gitignore
helloworld/Dockerfile
helloworld/README.md
helloworld/analysis_options.yaml
helloworld/bin/server.dart
helloworld/lib/functions.dart
helloworld/pubspec.yaml
helloworld/test/function_test.dart
8 files written.
--> to provision required packages, run 'pub get'

Finally, run pub get to install dependencies.

Run Your Function

After bootstrapping your function, you can run the function locally with:

dart run bin/server.dart

Then you can test the function at http://localhost:8080 using curl:

# curl http://localhost:8080
Hello, World!

Nice! 🙌

Deploy to Cloud Run

Now, let’s deploy to Cloud Run:

gcloud beta run deploy my-dart-function \
--source=. \
--region=us-central1 \
--platform managed \
--allow-unauthenticated

This command will follow the steps in the pre-generated Dockerfile to build your function.

Note: gcloud beta is needed for using the —-source flag.

After a minute or so, you’ll have deployed a Cloud Run service, in this case with a public URL:

https://my-dart-function-q7vieseilq-uc.a.run.app

Great! We just deployed an autoscaling function in Dart. 🎉

Learn More

If you enjoyed this blogpost, check out the docs in the source code:

https://github.com/GoogleCloudPlatform/functions-framework-dart

Or watch a video walking through building a QR code generator:

Cloud Functions with Dart Tutorial

--

--