2
0

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 3 years have passed since last update.

【Firebase / Flutter Web】firestoreEmulatorとローカル実行アプリを接続する。

Posted at

環境:Mac Catalina 10.15.7
開発対象:Flutter Web
エディター:Visual Studio Code

main.dartで起動コマンドのオプションにより、分岐させる処理を追加する。

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';


void main() async {

  const isEmulator = bool.fromEnvironment('IS_EMULATOR');//追記
  print('start(isEmulator: $isEmulator)');

  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

//以下追記 ↓  
  if (isEmulator) {
    /* 起動時にオプションをつけた場合→"--dart-define=IS_EMULATOR=true" */
    FirebaseFirestore.instance.settings = Settings(
      host: 'localhost:58080',//エミュレーターのローカルURL
      sslEnabled: false,
      persistenceEnabled: false,
    );
  }
//ここまで追記 ↑ 

  runApp(MyAppHome());
}

launch.jsonにオプション付きで登録しておく

launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "port1234",
            "request": "launch",
            "type": "dart",
            "a,rgs": ["-d", "chrome","--web-port", "1234"],
        },
     //以下追記
        {
            "name": "emu1234",
            "request": "launch",
            "type": "dart",
            "args": ["-d", "chrome","--web-port", "1234","--dart-define=IS_EMULATOR=true"],
        }
    ]
}

参考
https://qiita.com/someone7140/items/26fa2f5710b5b36e537f
https://medium.com/flutter-jp/firebase-emulator-938e9a0cdfad
https://www.filledstacks.com/post/how-to-setup-firebase-emulator-with-flutter/

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?