Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Flutter says ModalBottomSheetRoute is imported from two libraries

ErrorError: 'ModalBottomSheetRoute' is imported from both

Short 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.

  • Applies to Flutter 3.x, Dart 3.x, modal_bottom_sheet 3.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.lock
  2. 2Match what you see, then jump to its fix

    1. 1The error names the modal_bottom_sheet package as one of the two sourcesThe modal_bottom_sheet package still has its own ModalBottomSheetRouteFix
    2. 2The 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

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.lock

2Fix

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_sheet

3Verify

Analyze again: the ambiguity error should be gone.

flutter analyze

Confirmed by pub.dev

Did this fix it?

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 analyze

Confirmed by dart.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.