我们如何解决 flutter 应用程序中的 firebase 函数内部异常?

sah*_*hil 10 firebase flutter google-cloud-functions google-cloud-firestore

我正在使用 firebase firestore 和 firebase 函数开发一个 flutter 应用程序。我一次又一次地收到此异常 -

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: [firebase_functions/internal] INTERNAL
E/flutter (15454): #0      catchPlatformException (package:cloud_functions_platform_interface/src/method_channel/utils/exception.dart:19:3)
Run Code Online (Sandbox Code Playgroud)

自过去几个小时以来,我一直在尝试解决此异常,但无法取得任何进展。这是 index.js 文件中我的函数的代码。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

    exports.addUser = functions.https.onCall(async (data) => {
        await admin.firestore().collection("collection_name").add({
          name: "my_name",
          email: "my_email" 
        }
        );
    });
Run Code Online (Sandbox Code Playgroud)

这是我的 flutter 应用程序的 dart 代码。

                MyButton(
                  onPressed: () async {
                    HttpsCallable a =
                        FirebaseFunctions.instance.httpsCallable("addUser");
                    final x = await a();
                    print(x.data);  
                  },
                ),
Run Code Online (Sandbox Code Playgroud)

提前致谢 !

Rob*_*erg 5

我以为你只指客户端。但回顾一下 Yadu 所写的内容,您也应该在云函数中处理它。像这样的东西:

exports.addUser = functions.https.onCall(async (data) => {
  try {
    await admin.firestore().collection("collection_name").add({
      name: "my_name",
      email: "my_email" 
    }
    );
  } catch (err) {
    throw new functions.https.HttpsError('invalid-argument', "some message");
  }
});
Run Code Online (Sandbox Code Playgroud)

在客户端:

HttpsCallable a = FirebaseFunctions.instance.httpsCallable("addUser");

try {
  final x = await a();
  print(x.data);  
} on FirebaseFunctionsException catch (e) {
  // Do clever things with e
} catch (e) {
  // Do other things that might be thrown that I have overlooked
}
Run Code Online (Sandbox Code Playgroud)

您可以在https://firebase.google.com/docs/functions/callable#handle_errors上阅读更多相关信息

客户端描述可在“处理客户端错误”部分下找到

  • 感谢您的帮助,已经尝试这样做,但错误消息没有帮助 [firebase_functions/internal] INTERNAL。 (2认同)