Search code examples
flutterdartrouterflutter-dependenciesslidy

Ambiguous import after run slidy start



I've just started with Flutter and I've been looking for this problem and I didn't find anything.
I ran the command slidy start in order to create my project structure.But right now, I am getting this error import Router conflict in the App Module:

The name 'Router' is defined in the libraries 'package:flutter/src/widgets/router.dart' and 'package:flutter_modular/src/routers/router.dart (via package:flutter_modular/flutter_modular.dart)'.\nTry using 'as prefix' for one of the import directives, or hiding the name from all but one of the imports."

This is my class right now:

import 'app_controller.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:flutter/material.dart';
import 'package:slidy_aula2_v1/app/app_widget.dart';
import 'package:slidy_aula2_v1/app/modules/home/home_module.dart';

class AppModule extends MainModule {
  @override
  List<Bind> get binds => [
        Bind((i) => AppController()),
      ];

  @override
  List<Router> get routers => [
        Router(Modular.initialRoute, module: HomeModule()),
      ];

  @override
  Widget get bootstrap => AppWidget();

  static Inject get to => Inject<AppModule>.of();
}

Do you all know how to fix it?


Solution

  • You have two Router class in

    package:flutter/src/widgets/router.dart

    and

    package:flutter_modular/src/routers/router.dart

    Define a prefix to the flutter_modular package:

    import 'package:flutter_modular/flutter_modular.dart' as ModularRouter; 
    

    if you are using the Router class in flutter_modular, change Router to ModularRouter.Router.

    class AppModule extends MainModule {
      @override
      List<Bind> get binds => [
            Bind((i) => AppController()),
          ];
    
      @override
      List<ModularRouter.Router> get routers => [
            ModularRouter.Router(Modular.initialRoute, module: HomeModule()),
          ];
    
      @override
      Widget get bootstrap => AppWidget();
    
      static Inject get to => Inject<AppModule>.of();
    }