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

Flutter导入Lottie库失败求助:无法识别指定import语句

解决Flutter中Lottie库导入不存在的问题

1. 向项目添加Lottie依赖

打开项目根目录下的pubspec.yaml文件,找到dependencies代码块,添加Lottie的依赖声明:

dependencies:
  flutter:
    sdk: flutter
  # 添加这一行,版本号可以用最新稳定版,比如^2.6.0
  lottie: ^2.6.0

注:如果不确定最新版本,也可以直接写lottie: any,但推荐指定稳定版本避免兼容性问题。

2. 安装依赖

通过以下任意方式安装依赖:

  • 终端执行命令:flutter pub get
  • 若使用Android Studio或VS Code,直接点击pubspec.yaml文件右上角的「Pub get」按钮

3. 重新导入并使用

此时再执行import 'package:lottie/lottie.dart';就不会提示库不存在了,简单示例:

// 加载本地assets中的Lottie动画
Lottie.asset('assets/your_animation.json');
// 加载网络上的Lottie动画
Lottie.network('https://your-domain.com/animation.json');

常见问题排查

  • 检查pubspec.yaml的缩进:Flutter要求用2个空格缩进,不能使用Tab键,否则会导致依赖解析失败
  • 若flutter pub get报错,先检查网络连接,也可以尝试执行flutter pub cache repair修复本地缓存
  • 确认修改pubspec.yaml后已保存文件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 16:03:18