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

Flutter模拟器运行报错:modal_bottom_sheet依赖冲突求助

解决modal_bottom_sheet依赖引发的Flutter编译错误

问题场景

安装modal_bottom_sheet依赖后,运行Flutter模拟器时触发编译错误,应用无法正常构建。

完整报错信息

Launching lib\main.dart on SM A032M in debug mode...
../../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>(
            ^^^^^^^^^^^^^^^^^^^^^
../../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;
         ^
../../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>(
            ^^^^^^^^^^^^^^^^^^^^^
../../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;
         ^
../../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';
^^^^^^^^^^^^^^^^^^^^^
Target kernel_snapshot failed: Exception


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 41s
Exception: Gradle task assembleDebug failed with exit code 1

Exited (1).

当前导入代码

import 'dart:convert';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:weather/components/weather_item.dart';
import 'package:weather/constants.dart';
import 'package:weather/ui/detail_page.dart';

import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';

解决方案

1. 升级依赖到兼容版本

报错核心是你使用的modal_bottom_sheet:2.1.2版本过旧,与新版Flutter存在命名冲突(Flutter官方后续将ModalBottomSheetRoute纳入了material库),同时存在泛型返回类型的bug。

修改pubspec.yaml中的依赖版本:

dependencies:
  modal_bottom_sheet: ^3.0.0 # 或查看pub.dev获取最新稳定版

执行命令更新依赖:

flutter pub get

2. 清理缓存并重新构建

若升级后仍有残留问题,执行清理命令:

flutter clean
flutter pub get
flutter run

3. 命名导入规避冲突(可选,不推荐)

若暂时无法升级依赖,可通过命名导入解决命名冲突:
修改导入语句:

import 'package:modal_bottom_sheet/modal_bottom_sheet.dart' as modalSheet;

调用库中方法时添加前缀:

// 示例调用
modalSheet.showModalBottomSheet(
  context: context,
  builder: (context) => Container(),
);

内容的提问来源于stack exchange,提问作者LÓPEZ H

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 08:10:07