简体   繁体   English

Flutter - 添加 Firebase 消息时出现问题

[英]Flutter - Problem with adding Firebase Messaging

Doctor summary (to see all details, run flutter doctor -v):医生摘要(要查看所有详细信息,请运行 flutter 医生 -v):
[√] Flutter (Channel stable, 2.5.0, on Microsoft Windows [Versión 10.0.19042.1348], locale en-US) [√] Flutter(频道稳定,2.5.0,在 Microsoft Windows [版本 10.0.19042.1348],语言环境 en-US 上)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.0-rc2) [√] Android工具链-为Android设备开发(Android SDK版本30.0.0-rc2)
[√] Chrome - develop for the web [√] Chrome - 为 web 开发
[√] Android Studio (version 3.6) [√] Android工作室(3.6版)
[√] VS Code (version 1.52.1) [√] VS代码(1.52.1版本)
[√] Connected device (3 available) [√] 已连接设备(可用 3 个)

My pubsec.yaml has only:我的 pubsec.yaml 只有:
firebase_messaging: ^10.0.1 firebase_messaging:^10.0.1
firebase_core: ^1.2.1 firebase_core: ^1.2.1
flutter_local_notifications: ^6.1.0 flutter_local_notifications:^6.1.0

The problem I am having even on a brand new Flutter Project is that I am getting the following error when I add import 'package:firebase_messaging/firebase_messaging.dart';我什至在一个全新的 Flutter 项目中遇到的问题是,当我添加 import 'package:firebase_messaging/firebase_messaging.dart' 时出现以下错误; to main.dart到 main.dart

/D:/Flutter/flutter_windows_2.5.0-stable/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_messaging_platform_interface-3.2.0/lib/src/method_channel/utils/exception.dart:13:11: Error: Method not found: 'Error.throwWithStackTrace'.
    Error.throwWithStackTrace(exception, stackTrace);
          ^^^^^^^^^^^^^^^^^^^
/D:/Flutter/flutter_windows_2.5.0-stable/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_messaging_platform_interface-3.2.0/lib/src/method_channel/utils/exception.dart:16:9: Error: Method not found: 'Error.throwWithStackTrace'.
  Error.throwWithStackTrace(
        ^^^^^^^^^^^^^^^^^^^
/D:/Flutter/flutter_windows_2.5.0-stable/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_messaging_platform_interface-3.2.0/lib/src/method_channel/utils/exception.dart:11:7: Error: A non-null value must be returned since the return type 'Never' doesn't allow null.
Never convertPlatformException(Object exception, StackTrace stackTrace) {
      ^


FAILURE: Build failed with an exception.

* Where:
Script 'D:\Flutter\flutter_windows_2.5.0-stable\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1005

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'D:\Flutter\flutter_windows_2.5.0-stable\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 15s
Exception: Gradle task assembleDebug failed with exit code 1

Error.throwWithStackTrace was added in Dart 2.16 (Flutter version 2.10 from Feb 8, 2022). Dart 2.16(2022 年 2 月 8 日的 Flutter 2.10 版)中添加了 Error.throwWithStackTrace。

You should either:你应该:

  • Update Flutter to 2.10: flutter upgrade更新Flutter到2.10: flutter upgrade
  • Or use older version of conflicting package: firebase_messaging_platform_interface: 3.1.6或者使用旧版本的冲突 package: firebase_messaging_platform_interface firebase_messaging_platform_interface: 3.1.6

Tried all of the proposals, here and in the link here: https://github.com/FirebaseExtended/flutterfire/issues/8181在此处和此处的链接中尝试了所有建议: https://github.com/FirebaseExtended/flutterfire/issues/8181

But only that worked for me但只有那个对我有用

dependency_overrides:
  firebase_messaging_platform_interface: 3.1.6
  firebase_crashlytics_platform_interface: 3.1.13
  cloud_firestore_platform_interface: 5.4.13
  firebase_auth_platform_interface: 6.1.11
  firebase_storage_platform_interface: 4.0.14
  cloud_functions_platform_interface: 5.0.21
  firebase_analytics_platform_interface: 3.0.5
  firebase_remote_config_platform_interface: 1.0.5
  firebase_dynamic_links_platform_interface: 0.2.0+5
  firebase_performance_platform_interface: 0.1.0+5
  firebase_app_installations_platform_interface: 0.1.0+6

This are the dependencies I am using in first place这是我首先使用的依赖项

  firebase_core: ^1.6.0
  firebase_crashlytics: ^2.2.1
  firebase_messaging: ^10.0.9
  firebase_analytics: ^9.1.0

I have faced the same problem and got a workaround for this.我遇到了同样的问题,并为此找到了解决方法。

Add firebase_messaging_platform_interface pubspeck.yaml:添加 firebase_messaging_platform_interface pubspeck.yaml:

dependency_overrides:
  firebase_messaging_platform_interface: 3.1.6

There were changes made to the dependencies of the Flutter Firebase packages https://github.com/FirebaseExtended/flutterfire/pull/8156对 Flutter Firebase 包的依赖项进行了更改https://github.com/FirebaseExtended/flutterfire/pull/8156

You can either update to use >=2.16 version of dart Or override the dependencies, you only need to add the ones that you are using您可以更新以使用>=2.16版本的 dart 或者覆盖依赖项,您只需要添加您正在使用的那些

dependency_overrides:
  firebase_messaging_platform_interface: 3.1.6
  firebase_storage_platform_interface: 4.0.14
  cloud_functions_platform_interface: 5.0.21
  cloud_firestore_platform_interface: 5.4.13
  firebase_auth_platform_interface: 6.1.11
  firebase_database_platform_interface: 0.2.0+5

So I ran into this problem recently while using firebase_storage所以我最近在使用firebase_storage时遇到了这个问题

What I did to solve the problem was... (I was on flutter 2.6 and dart 2.14).我为解决问题所做的是......(我在 flutter 2.6 和 dart 2.14 上)。

Upgrade flutter and dart version: Into terminal, run flutter upgrade or in my case, flutter upgrade --force , since flutter upgrade was giving me some issues.升级 flutter 和 dart 版本:进入终端,运行flutter upgrade或在我的情况下, flutter upgrade --force ,因为 flutter upgrade 给我带来了一些问题。 Then I added the latest version of the dependencies to my pubspec (firebase_storage_10.2.0) At this stage, running the app will likely throw an error about unsupported compileSDKVersion or so, and ask you to upgrade.然后我将最新版本的依赖项添加到我的 pubspec (firebase_storage_10.2.0) 在此阶段,运行该应用程序可能会抛出有关不受支持的 compileSDKVersion 左右的错误,并要求您升级。 For that, go into your app level build.gradle file为此,go 进入您的应用程序级别 build.gradle 文件

("project"\android\app\build.gradle), ("项目"\android\app\build.gradle),

and under android, change the compileSDKVersion from it's current value to what flutter asks you to change to.在 android 下,将 compileSDKVersion 从当前值更改为 flutter 要求您更改的值。 in my case, it was from 30 to 31就我而言,它是从 30 到 31

android{
       //change compile sdk version to 31 (in my case. flutter will tell you which version you should set to)
       compileSdkVersion 31
}

When you run the app now, it will check for licenses for the Android SDK Package you just edited (31).当您现在运行该应用程序时,它将检查您刚刚编辑的 Android SDK Package 的许可证 (31)。 it should automatically accept and install everything neccessary.它应该自动接受并安装所有必要的东西。 Flutter run might then fail again, with the error of an incompatible kotlin version, and that you should update or so. Flutter 运行可能会再次失败,出现不兼容的错误 kotlin 版本,您应该更新左右。 To update the kotlin version, go to the project level build.gradle ("project"/android/build.gradle) change the kotlin version here (ext.kotlin_version = '1.3.5') to the latest version which can be found Here .要将 kotlin 版本 go 更新为项目级别 build.gradle ("project"/android/build.gradle) ,请将此处的 kotlin 版本(ext.kotlin_version = '1.3.5')更改为最新版本,可在此处找到。 as of now, the latest version is 1.6.10 so this line of code now reads截至目前,最新版本是 1.6.10 所以这行代码现在显示为

ext.kotlin_version = '1.6.10' ext.kotlin_version = '1.6.10'

Now you are good to go. run the app again, it might take much longer than usual, but it should work just fine.现在你可以拨打 go 了。再次运行该应用程序,它可能需要比平时更长的时间,但它应该可以正常工作。 Or at least it worked fine for me.或者至少对我来说效果很好。

I was obtaining the following error:我收到以下错误:

{flutter-folder}/.pub-cache/hosted/pub.dartlang.org/firebase_messaging_web-2.2.9/lib/src/internals.dart:11:20: Error: Method not found: 'guardWebExceptions'. {flutter-folder}/.pub-cache/hosted/pub.dartlang.org/firebase_messaging_web-2.2.9/lib/src/internals.dart:11:20:错误:找不到方法:'guardWebExceptions'。 return internals.guardWebExceptions( ^^^^^^^^^^^^^^^^^^返回 internals.guardWebExceptions( ^^^^^^^^^^^^^^^^^^^

I recently solved it adding firebase_messaging_web: 2.2.7 to my dependency_overrides section我最近解决了它,将 firebase_messaging_web: 2.2.7 添加到我的 dependency_overrides 部分

dependency_overrides:
  firebase_messaging_web: 2.2.7

You can see the package available versions at https://pub.dev/packages/firebase_messaging_web/versions您可以在https://pub.dev/packages/firebase_messaging_web/versions查看 package 可用版本

flutter --version flutter——版本

https://github.com/flutter/flutter.git<
Framework • revision 097d3313d8 (10 days ago) • 2022-02-18 19:33:08 -0600
Engine • revision a83ed0e5e3
Tools • Dart 2.16.1 • DevTools 2.9.2

pubspec.yml pubspec.yml

dependencies:
  firebase_messaging: 10.0.9

I hope you find this usefull.我希望你觉得这很有用。 I was facing this problem for a long time.我很长一段时间都面临着这个问题。

This throwWithStackTrace method was added in flutter's dart-core since 2.16 flutter version.这个throwWithStackTrace 方法是从 2.16 flutter 版本开始在 flutter 的 dart-core 中添加的。 The workaround would be adding the dependency_overrides just like how we add解决方法是像我们添加的那样添加 dependency_overrides

dependencies:
      flutter:
        sdk: flutter
      cupertino_icons: ^1.0.2

and

dev_dependencies:
  flutter_test:
    sdk: flutter

now we have to add dependency overrides in the same manner as dependencies and dev_dependencies.现在我们必须以与 dependencies 和 dev_dependencies 相同的方式添加依赖覆盖。 Example:例子:

dependency_overrides:
  firebase_messaging_platform_interface: 3.1.6
  firebase_crashlytics_platform_interface: 3.1.13
  cloud_firestore_platform_interface: 5.4.13
  firebase_auth_platform_interface: 6.1.11
  firebase_storage_platform_interface: 4.0.14
  cloud_functions_platform_interface: 5.0.21
  firebase_analytics_platform_interface: 3.0.5
  firebase_remote_config_platform_interface: 1.0.5
  firebase_dynamic_links_platform_interface: 0.2.0+5  
  firebase_performance_platform_interface: 0.1.0+5 
  firebase_app_installations_platform_interface: 0.1.0+6

Reference: Issue created in flutter's github参考:在 flutter 的 github 中创建的问题

Add into yaml:添加到 yaml:

 firebase_messaging_platform_interface

As suggested in this post( how to solve Execution failed for task ':app:compileFlutterBuildDebug' ) you should try to run in your terminal "Packages get | Pub get" and then build the project again正如这篇文章中所建议的( 如何解决任务 ':app:compileFlutterBuildDebug' 的执行失败),您应该尝试在终端中运行“Packages get | Pub get”,然后再次构建项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM