Firebase 函数 https 调用在模拟器上不起作用

Firebase Functions https call does not work on emulator

在 firebase 模拟器上执行 https 调用时,我只收到 FirebaseFunctionException。除 https 调用外,其他所有功能都可以正常工作。在没有模拟器的情况下连接到 firebase 时,一切正常。我无法在互联网上找到解决方案,有人可以帮忙吗?

方法调用:

    try {
      HttpsCallable callable =
          FirebaseFunctions.instance.httpsCallable('getBookData');
      final results = await callable.call(parameters);

初始化:

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

...

 final localHostString = Platform.isAndroid ? '10.0.2.2' : 'localhost';

    // dotenv.get('HOST_IP');

    FirebaseFunctions.instance
        .useFunctionsEmulator('http://$localHostString', 5001);

    FirebaseFirestore.instance.settings = Settings(
      host: '$localHostString:8080',
      sslEnabled: false,
      persistenceEnabled: false,
    );

    await auth.FirebaseAuth.instance.useEmulator(
        Platform.isAndroid ? 'http://localhost:9099' : 'http://0.0.0.0:9099');
  }

异常:

code:"unavailable"
details:null
message:"UNAVAILABLE"
plugin:"firebase_functions"
stackTrace:#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
<asynchronous suspension>
#2      MethodChannelHttpsCallable.call (package:cloud_functions_platform_interface/src/method_channel/method_channel_https_callable.dart:23:24)
<asynchronous suspension>
#3      HttpsCallable.call (package:cloud_functions/src/https_callable.dart:35:37)
<asynchronous suspension>
#4      BookRequest._requestBooksFromCloudFunction (package:bookR/shared/bookRequest.dart:47:23)
<asynchronous suspension>
#5      BookRequest.getMoreBooksByTitle (package:bookR/shared/bookRequest.dart:23:24)
<asynchronous suspension>
#6      BookRequest.getBookInformationByTitle (package:bookR/shared/bookRequest.dart:37:13)
<asynchronous suspension>
#7      SearchBar.build.<anonymous closure> (package:bookR/widgets/searchBar.dart:34:28)
<asynchronous suspension>
hashCode:439854717
runtimeType:Type (FirebaseFunctionsException)

我终于明白了! :D 万一以后有人发现这个:

FirebaseFunctions.instance
        .useFunctionsEmulator('http://$localHostString', 5001);

这会导致错误。正确的版本是:

FirebaseFunctions.instance
        .useFunctionsEmulator('$localHostString', 5001);

现在 erverthing 正常工作了