Flutter升级后项目无法运行,依赖问题解决后仍报错怎么办?
问题描述
已解决依赖问题,但运行Flutter项目时出现一系列与Flutter库API相关的错误,执行flutter clean和flutter pub get后问题未改善。
项目环境配置
environment: sdk: '>=2.18.2 <3.0.0'
当前Flutter版本
$ flutter --version Flutter 3.10.5 • channel stable • https://github.com/flutter/flutter.git Framework • revision 796c8ef792 (8 days ago) • 2023-06-13 15:51:02 -0700 Engine • revision 45f6e00911 Tools • Dart 3.0.5 • DevTools 2.23.1
flutter doctor检测结果
$ flutter doctor Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.10.5, on macOS 12.6.2 21G320 darwin-x64, locale fr-FR) [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 14.2) [✓] Chrome - develop for the web [✓] Android Studio (version 2021.1) [✓] VS Code (version 1.79.1) [✓] Connected device (3 available) [✓] Network resources • No issues found!
报错信息
Launching lib/main.dart on Pixel 7 in debug mode... main.dart:1 : Error: The method 'ExtendedPagePosition.copyWith' doesn't have the named parameter 'devicePixelRatio' of overridden method 'ViewportOffset with ScrollMetrics.copyWith'. scroll_position.dart:184 PageMetrics copyWith({ ^ : Context: This is the overridden method ('copyWith'). scroll_position.dart:92 abstract class ScrollPosition extends ViewportOffset with ScrollMetrics { ^ : Error: The method 'ExtendedPagePosition.copyWith' has fewer named arguments than those of overridden method 'PageMetrics.copyWith'. scroll_position.dart:184 PageMetrics copyWith({ ^ : Context: This is the overridden method ('copyWith'). page_view.dart:277 PageMetrics copyWith({ ^ : Error: The method 'ExtendedPagePosition.copyWith' doesn't have the named parameter 'devicePixelRatio' of overridden method 'PageMetrics.copyWith'. scroll_position.dart:184 PageMetrics copyWith({ ^ : Context: This is the overridden method ('copyWith'). page_view.dart:277 PageMetrics copyWith({ ^ ^^^^^^^^^^^ : Error: The getter 'buttonColor' isn't defined for the class 'ThemeData'. build_color.dart:109 - 'ThemeData' is from 'package:flutter/src/material/theme_data.dart' ('../../flutter/packages/flutter/lib/src/material/theme_data.dart'). theme_data.dart:1 Try correcting the name to the name of an existing getter, or defining a getter or field named 'buttonColor'. Color get button => Theme.of(_context).buttonColor; ^^^^^^^^^^^ : Error: Required named parameter 'devicePixelRatio' must be provided. gesture_page_view.dart:21 final PageMetrics _testPageMetrics = PageMetrics( ^ : Context: Found this candidate, but the arguments don't match. page_view.dart:266 PageMetrics({ ^^^^^^^^^^^ : Error: The superclass, 'OneSequenceGestureRecognizer with DragGestureRecognizerMixin', has no unnamed constructor that takes no arguments. drag.dart:145 ExtendedDragGestureRecognizer({ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ : Error: Required named parameter 'devicePixelRatio' must be provided. scroll_position.dart:192 return PageMetrics( ^ : Context: Found this candidate, but the arguments don't match. page_view.dart:266 PageMetrics({ ^^^^^^^^^^^ Target kernel_snapshot failed: Exception
解决方案
这些错误本质是第三方包与当前Flutter版本不兼容导致的——Flutter 3.10.x对PageMetrics、ThemeData等API做了变动,而项目依赖的部分第三方包未适配这些更新。按以下步骤修复:
定位不兼容的第三方包
报错中提到的scroll_position.dart、gesture_page_view.dart均非Flutter官方文件,而是来自第三方扩展包(大概率是extended_page_view或类似功能的包)。先在pubspec.yaml中找到这类依赖并标记。升级或替换不兼容包
- 打开
pubspec.yaml,将对应包的版本号改为最新稳定版(格式如^x.y.z),执行flutter pub upgrade拉取适配版本。 - 如果包已停止维护、无适配版本,直接替换为功能类似的活跃维护包(比如用官方
PageView替代第三方扩展版)。
- 打开
修复
ThemeData.buttonColor报错
Flutter新版本移除了buttonColor,可根据使用场景替换为colorScheme中的对应颜色:// 替换前 Color get button => Theme.of(_context).buttonColor; // 替换后示例(按需选择) Color get button => Theme.of(_context).colorScheme.onPrimary; // 或针对按钮组件的主题颜色 Color get button => Theme.of(_context).elevatedButtonTheme.style?.backgroundColor?.resolve({}) ?? Colors.blue;彻底重置依赖
执行以下命令清理旧依赖缓存并重新拉取:flutter clean rm -rf pubspec.lock flutter pub get更新SDK版本约束
当前Dart版本为3.0.5(随Flutter 3.10.x自带),但项目SDK约束是<3.0.0,可能导致部分包适配问题。修改pubspec.yaml中的环境约束:environment: sdk: '>=3.0.0 <4.0.0'之后重新执行
flutter pub get。
内容的提问来源于stack exchange,提问作者Stéphane de Luca
相关产品推荐
相关产品推荐

