Flutter says ModalBottomSheetRoute is imported from two libraries
Error: 'ModalBottomSheetRoute' is imported from bothShort answer
Two libraries you import both declare a class named ModalBottomSheetRoute: Flutter's material library and, usually, the modal_bottom_sheet package. Upgrade the package to a release that renamed its class to ModalSheetRoute. If you cannot upgrade yet, import one library with hide or an as prefix so the name is only visible from one.
Fix it fast
1Find out which cause you have
Check whether the modal_bottom_sheet package is in your project and which version you resolve to.
grep -n "modal_bottom_sheet" pubspec.yaml pubspec.lock2Match what you see, then jump to its fix
- 1
The error names the modal_bottom_sheet package as one of the two sourcesThe modal_bottom_sheet package still has its own ModalBottomSheetRouteFix - 2
The error lists two libraries, and the one that is not Flutter is a package you cannot upgrade yetTwo imports bring in the same name and you cannot change either libraryFix
- 1
What does the error look like?
Error: 'ModalBottomSheetRoute' is imported from both
Cause 1 of 2
The modal_bottom_sheet package still has its own ModalBottomSheetRoute
1Check
Flutter's material library has its own ModalBottomSheetRoute (it is what showModalBottomSheet pushes). The package's changelog says it renamed its class to ModalSheetRoute so the two no longer clash, starting at 3.0.0-pre for Flutter 3.7. An older package version still ships the clashing name.
grep -n -A2 "modal_bottom_sheet:" pubspec.lock2Fix
Raise the constraint to a 3.x release and update the code that used the old class name to ModalSheetRoute.
# pubspec.yaml
dependencies:
modal_bottom_sheet: ^3.0.0
flutter pub upgrade modal_bottom_sheet3Verify
Analyze again: the ambiguity error should be gone.
flutter analyzeConfirmed by pub.dev
Cause 2 of 2
Two imports bring in the same name and you cannot change either library
1Check
Dart reports a conflict when two imported libraries expose the same identifier. Find the import lines in the file named by the error.
grep -rn "import 'package:flutter/material.dart'\|modal_bottom_sheet" lib/2Fix
Make the name visible from one library only. hide removes it from one import; an as prefix keeps both but you qualify one of them.
// keep the package's class, hide Flutter's
import 'package:flutter/material.dart' hide ModalBottomSheetRoute;
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
// or keep both and qualify the package's one
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart' as mbs;
// mbs.ModalBottomSheetRoute(...)3Verify
Run the analyzer on the file again.
flutter analyzeConfirmed by dart.dev
How do I stop it coming back?
- Keep dependencies current; a plugin that is a few Flutter versions behind is the usual source of name clashes.
- When you add an import you only need one class from, use show so you do not pull in extra names.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix