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

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 clean
  • flutter upgrade
  • pub upgrade
  • 使用pub outdated并升级大版本依赖
  • 清除Flutter/Pub缓存
  • 删除项目中的.gradle、local.properties、.darttool、.idea文件

解决方案

错误根源是modal_bottom_sheet包2.1.2版本与当前Flutter版本存在兼容性冲突,可按以下方式处理:

  1. 优先升级包版本:在项目pubspec.yaml中指定该包的最新兼容版本,示例:
    modal_bottom_sheet: ^3.0.0
    
    执行flutter pub get拉取新依赖后重新构建。
  2. 临时修改包源码(应急用):
    • 找到缓存中的包路径: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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 08:05:38