Flutter项目构建、调试报错,执行常规修复操作后仍无法解决
Flutter构建调试报错处理
错误信息
Launching lib\main.dart on SM M115F in debug mode... Running Gradle task 'assembleDebug'... /C:/Users/91984/AppData/Local/Pub/Cache/hosted/pub.dev/modal_bottom_sheet-2.1.2/lib/src/material_with_modal_page_route.dart:4:1: Error: 'ModalBottomSheetRoute' is imported from both 'package:flutter/src/material/bottom_sheet.dart' and 'package:modal_bottom_sheet/src/bottom_sheet_route.dart'. import '../modal_bottom_sheet.dart'; ^^^^^^^^^^^^^^^^^^^^^ /C:/Users/91984/AppData/Local/Pub/Cache/hosted/pub.dev/modal_bottom_sheet-2.1.2/lib/src/bottom_sheets/material_bottom_sheet.dart:28:13: Error: 'ModalBottomSheetRoute' is imported from both 'package:flutter/src/material/bottom_sheet.dart' and 'package:modal_bottom_sheet/src/bottom_sheet_route.dart'. .push(ModalBottomSheetRoute<T>( ^^^^^^^^^^^^^^^^^^^^^ /C:/Users/91984/AppData/Local/Pub/Cache/hosted/pub.dev/modal_bottom_sheet-2.1.2/lib/src/bottom_sheets/material_bottom_sheet.dart:50:10: Error: A value of type 'Object?' can't be returned from an async function with return type 'Future<T?>'. - 'Object' is from 'dart:core'. - 'Future' is from 'dart:async'. return result; ^ /C:/Users/91984/AppData/Local/Pub/Cache/hosted/pub.dev/modal_bottom_sheet-2.1.2/lib/src/bottom_sheets/bar_bottom_sheet.dart:102:13: Error: 'ModalBottomSheetRoute' is imported from both 'package:flutter/src/material/bottom_sheet.dart' and 'package:modal_bottom_sheet/src/bottom_sheet_route.dart'. .push(ModalBottomSheetRoute<T>( ^^^^^^^^^^^^^^^^^^^^^ /C:/Users/91984/AppData/Local/Pub/Cache/hosted/pub.dev/modal_bottom_sheet-2.1.2/lib/src/bottom_sheets/bar_bottom_sheet.dart:125:10: Error: A value of type 'Object?' can't be returned from an async function with return type 'Future<T?>'. - 'Object' is from 'dart:core'. - 'Future' is from 'dart:async'. return result; ^ Target kernel_snapshot failed: Exception FAILURE: Build failed with an exception. * Where: Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1151 * What went wrong: Execution failed for task ':app:compileFlutterBuildDebug'. > Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1
已尝试操作
flutter cleanflutter upgradepub upgrade- 使用
pub outdated并升级大版本依赖 - 清除Flutter/Pub缓存
- 删除项目中的
.gradle、local.properties、.darttool、.idea文件
解决方案
错误根源是modal_bottom_sheet包2.1.2版本与当前Flutter版本存在兼容性冲突,可按以下方式处理:
- 优先升级包版本:在项目
pubspec.yaml中指定该包的最新兼容版本,示例:
执行modal_bottom_sheet: ^3.0.0flutter pub get拉取新依赖后重新构建。 - 临时修改包源码(应急用):
- 找到缓存中的包路径:
C:/Users/91984/AppData/Local/Pub/Cache/hosted/pub.dev/modal_bottom_sheet-2.1.2/ - 在
material_with_modal_page_route.dart、material_bottom_sheet.dart、bar_bottom_sheet.dart中,修改导入语句给冲突类加别名:
直接使用包内的import 'package:flutter/src/material/bottom_sheet.dart' as flutter_bottom_sheet; import '../modal_bottom_sheet.dart';ModalBottomSheetRoute即可避免冲突。 - 对类型不匹配错误,将
return result;修改为return result as T?;完成类型转换。
- 找到缓存中的包路径:
注:临时修改源码无法长期维护,后续包更新会覆盖修改,优先选择升级包版本。
内容的提问来源于stack exchange,提问作者Sathish Kumar
相关产品推荐
相关产品推荐

