6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Cloud FunctionsでDartを使う

Posted at

はじめに

firebase_functions_interopというライブラリ使ってDartでCloud Functions動かせたので、その共有です。

READMEに書かれている通りに進めるだけでとりあえずは動かせます。

firebase_functions_interopはdart2jsでdartからjsに変換してnodeで動かす作りになっています(Cloud FunctionsがDartに対応してないため。本来はサーバサイドはDart VMで動く作りのはず)。

DartをCloud Functionsで動かすところまで

ディレクトリ作成

ディレクトリ作成して、firebase init functionsでCloud Functionsの準備

$ mkdir cloud-functions-dart-example
$ cd cloud-functions-dart-example
$ firebase init functions

依存ライブラリ準備

functionsディレクトリ内にpubspec.yamlを準備

functions/pubspec.yaml
name: cloud_functions_dart_example
version: 0.0.1

environment:
  sdk: '>=2.0.0 <3.0.0'

dependencies:
  firebase_functions_interop: ^1.0.0

dev_dependencies:
  build_runner: ^1.0.0
  build_node_compilers: ^0.2.1

$ pub get

Hello World関数を追加

functions/node/index.dart
import 'package:firebase_functions_interop/firebase_functions_interop.dart';

void main() {
  functions['helloWorld'] = functions.https.onRequest(helloWorld);
}

void helloWorld(ExpressHttpRequest request) {
  request.response.writeln('Hello world');
  request.response.close();
}

ビルド

functionsディレクトリ内にbuild.yamlを準備

functions/build.yaml
targets:
  $default:
    sources:
      - "node/**"
      - "lib/**"
    builders:
      build_node_compilers|entrypoint:
        generate_for:
        - node/**
        options:
          compiler: dart2js
          dart2js_args:
          - --minify

functionsディレクトリ内でpubコマンドでビルド

$ pub run build_runner build --output=build

デプロイ

functionsディレクトリ内のpackage.jsonにmain追記

functions/package.json
{
    "...": "...",
    "main": "build/node/index.dart.js"
}

デプロイ

$ firebase deploy --only functions

おわりに

↓のリポジトリに動かしてみたやつおいてあります。
cloud-functions-dart-example

Flutterでなにか作りたいやー

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?