You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter项目添加sendgrid_mailer遇依赖冲突,求解决方案及替代包

Flutter依赖冲突:sendgrid_mailer与google_fonts的http版本不兼容问题

我正在开发一个Flutter项目,尝试添加sendgrid_mailer依赖时遇到了问题。以下是当前pubspec.yaml中的依赖:

dependencies:
  cupertino_icons: ^1.0.8
  firebase_auth: ^4.19.5
  firebase_core: ^2.31.0
  flutter:
    sdk: flutter
  google_fonts: ^6.2.1
  intl: ^0.19.0
  mailer: ^6.1.0
  path: ^1.9.0
  path_provider: ^2.1.3
  syncfusion_flutter_pdf: ^25.2.5
  syncfusion_flutter_pdfviewer: any

错误信息如下:

flutter pub add sendgrid_mailer
The current Dart SDK version is 3.4.1.

Because google_fonts >=4.0.5 depends on http ^1.0.0 and sendgrid_mailer >=0.1.3 depends on http ^0.13.0, google_fonts >=4.0.5 is incompatible with sendgrid_mailer >=0.1.3.
And because sendgrid_mailer <0.1.3 doesn't support null safety, google_fonts >=4.0.5 is incompatible with sendgrid_mailer.
So, because diagnosis depends on both google_fonts ^6.2.1 and sendgrid_mailer any, version solving failed.

The lower bound of "sdk: '>=2.7.0 <3.0.0'" must be 2.12.0 or higher to enable null safety.

请问是否有办法解决这个依赖冲突?如果没有,有没有可替代sendgrid_mailer的Flutter邮件发送包?


解决依赖冲突的方法

1. 强制统一http版本(风险需自测)

在pubspec.yaml中添加dependency_overrides,将http版本锁定为google_fonts要求的^1.0.0,强制sendgrid_mailer使用该版本:

dependency_overrides:
  http: ^1.0.0

执行flutter pub get后,务必测试sendgrid_mailer的核心功能是否正常运行。如果出现运行时错误,说明版本不兼容,需放弃此方案。

2. 降级google_fonts到兼容版本

根据错误提示,google_fonts >=4.0.5才依赖http ^1.0.0,可降级到4.0.4版本:

google_fonts: ^4.0.4

执行flutter pub get后再添加sendgrid_mailer即可。注意降级会丢失google_fonts新版本的功能,需根据项目需求权衡。

替代sendgrid_mailer的方案

如果上述方法不可行,可选择以下替代方案:

  • 使用已依赖的mailer包:mailer支持SMTP协议,可直接配置SendGrid的SMTP信息(在SendGrid后台创建API密钥,按mailer的SMTP配置规则填写即可)实现邮件发送。
  • 直接调用SendGrid API:用项目已有的http包封装请求逻辑,直接调用SendGrid的REST API发送邮件,完全规避依赖冲突问题。

内容的提问来源于stack exchange,提问作者Ziad Ghanem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 03:06:09