如何从 Flutter 调用本地 Firebase 函数(在模拟器中)?

Kia*_*ian 2 firebase flutter google-cloud-functions google-cloud-firestore

我在本地模拟器中设置了 Cloud Functions。我编写了一些函数并使用函数模拟器在浏览器中测试了它们。现在我尝试从本地 Flutter 应用程序调用这些函数而不部署它们。但是,我找不到将我的 Flutter 应用程序连接到函​​数的 localhost API 的方法。有办法做到这一点吗?

Fra*_*len 6

步骤:

  1. 导入包:

    import 'package:cloud_functions/cloud_functions.dart';
    
    Run Code Online (Sandbox Code Playgroud)
  2. 创建实例:

    static final _functions = CloudFunctions.instance;
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将其连接到模拟器:

    final origin = Platform.isAndroid ? 'http://10.0.2.2:5001' : 'http://localhost:5001';
    _functions.useFunctionsEmulator(origin: origin);
    
    Run Code Online (Sandbox Code Playgroud)

另请参阅:

  • 必须从“FirebaseFunctions”实例化,不再从“CloudFunctions”实例化 (3认同)