flutterdart

Error: 'CarouselController' is imported from both Package in Flutter


I tried to update my project but not able to solve this error. **I have tried using Alias - "as" keyword inside my code

import 'package:flutter_carousel_widget/flutter_carousel_widget.dart' as fcw;

    final fcw.CarouselController carouselController = fcw.CarouselController();

But now the due to package but it is working fine in their repo example app and tried using different versions of this package but getting same error.

environment: sdk: '>=2.18.0-246.0.dev <3.0.0'

Package link - https://pub.dev/packages/flutter_carousel_widget

Error :

/C:/Users/hp/AppData/Local/Pub/Cache/hosted/pub.dev/flutter_carousel_widget-2.1.0/lib/src/helpers/flutter_carousel_options.dart:5:1: Error: 'CarouselController' is imported from both 'package:flutter/src/material/carousel.dart' and 'package:flutter_carousel_widget/src/helpers/flutter_carousel_controller.dart'.
import 'package:flutter_carousel_widget/src/helpers/flutter_carousel_controller.dart';
^^^^^^^^^^^^^^^^^^
/C:/Users/hp/AppData/Local/Pub/Cache/hosted/pub.dev/flutter_carousel_widget-2.1.0/lib/src/_expandable_carousel_widget.dart:171:13: Error: 'CarouselController' is imported from both 'package:flutter/src/material/carousel.dart' and 'package:flutter_carousel_widget/src/helpers/flutter_carousel_controller.dart'.
          : CarouselController() as CarouselControllerImpl;
            ^^^^^^^^^^^^^^^^^^
/C:/Users/hp/AppData/Local/Pub/Cache/hosted/pub.dev/flutter_carousel_widget-2.1.0/lib/src/_flutter_carousel_widget.dart:143:13: Error: 'CarouselController' is imported from both 'package:flutter/src/material/carousel.dart' and 'package:flutter_carousel_widget/src/helpers/flutter_carousel_controller.dart'.
          : CarouselController() as CarouselControllerImpl;
            ^^^^^^^^^^^^^^^^^^
Unhandled exception:
Unsupported operation: Unsupported invalid type InvalidType(<invalid>) (InvalidType). Encountered while compiling file:///C:/Users/hp/AppData/Local/Pub/Cache/hosted/pub.dev/flutter_carousel_widget-2.1.0/lib/src/helpers/flutter_carousel_options.dart, which contains the type: FunctionType(CarouselOptions Function({allowImplicitScrolling: bool?, aspectRatio: double?, autoPlay: bool?, autoPlayAnimationDuration: Duration?, autoPlayCurve: Curve?, autoPlayInterval: Duration?, carouselController: <invalid>, clipBehavior: Clip?, disableCenter: bool?, dragStartBehavior: DragStartBehavior?, enableInfiniteScroll: bool?, enlargeCenterPage: bool?, enlargeStrategy: CenterPageEnlargeStrategy?, floatingIndicator: bool?, height: double?, indicatorMargin: double?, initialPage: int?, keepPage: bool?, onPageChanged: dynamic Function(int, CarouselPageChangedReason)?, onScrolled: void Function(double?)?, padEnds: bool?, pageSnapping: bool?, pageViewKey: PageStorageKey<dynamic>?, pauseAutoPlayInFiniteScroll: bool?, pauseAutoPlayOnManualNavigate: bool?, pauseAutoPlayOnTouch: bool?, physics: ScrollPhysics?, restorationId: String?, reverse: bool?, scrollBehavior: ScrollBehavior?, scrollDirection: Axis?, showIndicator: bool?, slideIndicator: SlideIndicator?, viewportFraction: double?})).
#0      ProgramCompiler._typeCompilationError (package:dev_compiler/src/kernel/compiler.dart:3224)
#1      ProgramCompiler._emitType (package:dev_compiler/src/kernel/compiler.dart:3185)
#2      ProgramCompiler._emitClassSignature (package:dev_compiler/src/kernel/compiler.dart:1560)
#3      ProgramCompiler._emitClassDeclaration (package:dev_compiler/src/kernel/compiler.dart:962)
#4      ProgramCompiler._emitClass (package:dev_compiler/src/kernel/compiler.dart:819)
#5      List.forEach (dart:core-patch/growable_array.dart:417)
#6      ProgramCompiler._emitLibrary (package:dev_compiler/src/kernel/compiler.dart:757)
#7      List.forEach (dart:core-patch/growable_array.dart:417)
#8      ProgramCompiler.emitModule (package:dev_compiler/src/kernel/compiler.dart:486)
#9      IncrementalJavaScriptBundler.compile (package:frontend_server/src/javascript_bundle.dart:221)
#10     FrontendCompiler.writeJavaScriptBundle (package:frontend_server/frontend_server.dart:870)
<asynchronous suspension>
#11     FrontendCompiler.compile (package:frontend_server/frontend_server.dart:685)
<asynchronous suspension>
#12     listenAndCompile.<anonymous closure> (package:frontend_server/frontend_server.dart:1385)
<asynchronous suspension>
Finished with error: the Dart compiler exited unexpectedly.
Failed to compile application.

Error is due to the package code because they have imported both package inside their file as per my knowledge due then how their example app is working Package imports:

import 'package:flutter/material.dart';
import'package:flutter_carousel_widget/src/helpers/flutter_carousel_state.dart';

Error

 pub.dev/flutter_carousel_widget-2.1.0/lib/src/helpers/flutter_carousel_options.dart:5:1: Error: 'CarouselController' is imported from both 'package:flutter/src/material/carousel.dart' and 'package:flutter_carousel_widget/src/helpers/flutter_carousel_controller.dart'.
    import 'package:flutter_carousel_widget/src/helpers/flutter_carousel_controller.dart';

Solution

  • Yes, the error is due to package code conflicting as CarouselController is Defined in both material and flutter_carousel_widget. I also faced the same issue and resolved it. https://github.com/nixrajput/flutter_carousel_widget/pull/47

    Till new Updates are not released you can use this Repo https://github.com/amarjeetpatidar007/flutter_carousel_widget