“[firebase_functions/unavailable] UNAVAILABLE Exception”使用带有云功能的 Firebase 本地模拟器

问题描述 投票:0回答:2

我正在尝试使用 Functions Emulator,但当我尝试在 Flutter 应用程序中使用它时,它会导致问题。我目前使用的是免费计划,但我已经了解到可以使用本地模拟器的功能。

当我像这样创建函数(使用节点 v2)时:

exports.getBooks = onRequest(async (req, res) => {
...
}

然后从浏览器发出 HTTP 请求,我得到了想要的结果。但是,当我将其更改为

const {onCall, onRequest} = require("firebase-functions/v2/https");
const logger = require("firebase-functions/logger");
const {getFirestore} = require("firebase-admin/firestore");
const admin = require("firebase-admin");
const app = admin.initializeApp();
const db = getFirestore(app);

exports.getBooks = onCall(async (request) => {
...
}

然后从我的 Flutter 应用程序进行函数调用,我收到一个 UNAVAILABLE 异常。

我在 main.dart 中添加了以下代码:

WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);

FirebaseFunctions.instance.useFunctionsEmulator('localhost', 5001);

我的应用程序中的代码:

final HttpsCallable getBooks = FirebaseFunctions.instance.httpsCallable('getBooks');
final response = await getBooks.call();
// OR without .call(): final response = await getBooks();

但是,

.call()
方法导致了这个问题。

我已将

android:usesCleartextTraffic="true"
添加到 AndroidManifest.xml 中的
<application>
,但它无法解决问题。

您对如何实现这项工作有任何想法吗?

相关链接

[不适用]

flutter firebase exception firebase-tools
2个回答
0
投票

尝试将 localhost 替换为 10.0.2.2。

此外,如果您的功能部署在任何特定区域,请也指定该区域,如下所示。

FirebaseFunctions.instanceFor(region: 'us-central1')
  .useFunctionsEmulator('10.0.2.2', 5001);

FirebaseFunctions.instanceFor(region: 'us-central1').httpsCallable('getBooks');

上述更改应该有效。


0
投票

对我来说,问题是

AndroidManifest.xml
默认情况下会阻止所有未加密的流量。在我的
AndroidManifest.xml
中添加以下内容允许我访问本地模拟器(在客户端中使用 ip
10.0.2.2
):

<application
....
android:usesCleartextTraffic="true"
....>

但是,请确保在将函数部署到 Firebase 后从

AndroidManifest.xml
中删除上述部分。

© www.soinside.com 2019 - 2024. All rights reserved.