颤动+Firebase:配置应用程序以使用本地Firebase模拟器

人气:699 发布:2022-10-16 标签: firebase flutter google-cloud-functions

问题描述

我已通过this链接将Firebase配置为在本地运行,以便使用仿真程序进行调试。

现在,我还希望能够运行连接到本地主机的应用程序以调试触发器。有没有办法通过将我的Ffltter应用程序配置为使用本地主机来实现这一点?

我的模拟器正在运行,如下所示:

推荐答案

我最新的扑火设置

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  
  await Firebase.initializeApp();

  const bool USE_EMULATOR = true;

  if (USE_EMULATOR) {
    // [Firestore | localhost:8080]
    FirebaseFirestore.instance.settings = const Settings(
      host: 'localhost:8080',
      sslEnabled: false,
      persistenceEnabled: false,
    );
    
    // [Authentication | localhost:9099]
    await FirebaseAuth.instance.useEmulator('http://localhost:9099');

    // [Storage | localhost:9199]
    await FirebaseStorage.instance.useEmulator(
      host: 'localhost',
      port: 9199,
    );
  }
}
确保您的主机和端口与firebase emulators:start匹配

注意:在main.dart中,现在您始终可以提供‘Localhost’

await FirebaseAuth.instance.useEmulator('http://localhost:9099');

因为如果在Android上运行,它将自动更改为‘10.0.2.2’

长话短说!

有关最新指南,请参阅https://firebase.flutter.dev/docs/firestore/usage#emulator-usage

老但是,黄金。详细配置(过时)

第1步[在主扑翼中设置Firestore。省道]

Future<void> main() async {
  
    WidgetsFlutterBinding.ensureInitialized(); <--- Important!
      
    await Firestore.instance.settings(
            host: '192.168.1.38:5002', <--- Make sure to put your local ip 
            sslEnabled: false);             it will not work if you use 'localhost:5002'
                                            Google it "how to find my local ip"
       
}

第1步[在主扑翼中设置FireStore]用于更新版本的FireBase

Future<void> main() async {
  
    WidgetsFlutterBinding.ensureInitialized(); <--- Important!
   
    String host = Platform.isAndroid ? '10.0.2.2:5002' : 'localhost:5002';
    await FirebaseFirestore.instance.settings = Settings(
         host: host,
         sslEnabled: false,
    );  
       
}

第二步[初始化Firebase项目]

firebase init

第3步[配置FireStore模拟器,例如Firebase.json]

"emulators": {
    "ui": {
      "enabled": true,
      "host": "localhost",
      "port": 4000
    },
    "functions": {
      "port": 5001
    },
    "firestore": {
      "host": "0.0.0.0", <------ Make sure to set it "0.0.0.0"
      "port": 5002
    },
}

第4步[运行仿真器和颤动应用程序]

firebase emulators:start
flutter run

同时在iOS模拟器和Android模拟器上工作

备注:尝试重启Firestore仿真器或/和FIFTH应用程序

完成!

Bonus[将导出数据导入到FireStore仿真器]

当您停止FireStore模拟器时,FireStore中的所有数据都将消失。 所以在停止模拟器之前,如果你想从哪里继续 您可以像这样导出FireStore仿真器数据

firebase emulators:export ../data(../可以是您想要的任何路径)

加载导出的数据

firebase emulators:start --import ../data

您可以根据不同情况保存FireStore模拟器的不同状态,例如

firebase emulators:start --import ../initialData 
firebase emulators:start --import ../otherStateData

❤️注意事项将DART用于Firebase函数❤️

如果要将DART用于Firebase功能,可以遵循https://github.com/pulyaevskiy/firebase-functions-interop

我自己发现了一件好事,可以检测您的函数是在仿真器还是生产中运行here

长话短说

functions/index.js

export const prepopulateFirestoreEmulator = functions.https.onRequest(
  (request, response) => {
    if (process.env.FUNCTIONS_EMULATOR && process.env.FIRESTORE_EMULATOR_HOST) {
      // TODO: prepopulate firestore emulator from 'yourproject/src/sample_data.json
      response.send('Prepopulated firestore with sample_data.json!');
    } else {
      response.send(
        "Do not populate production firestore with sample_data.json"
      );
    }
  }
);

函数/index.dart

import 'package:firebase_functions_interop/firebase_functions_interop.dart';
import 'package:node_interop/node.dart';
import 'package:node_interop/util.dart';

void devPrepopulateCollections(ExpressHttpRequest request) {
  var env =
      new Map<String, String>.from(dartify(process.env)); // <-- important part

  if (env.containsKey('FUNCTIONS_EMULATOR') &&
      env.containsKey('FIRESTORE_EMULATOR_HOST')) {
    // TODO: prepopulate firestore emulator from 'yourproject/src/sample_data.json
    request.response
      ..write("Prepopulated firestore with sample_data.json!")
      ..close();
  } else {
    request.response
      ..write("Do not populate production firestore with sample_data.json")
      ..close();
  }
}

499