Home > Net >  Type 'Router' not found.Method not found: 'Router',even I've imported
Type 'Router' not found.Method not found: 'Router',even I've imported

Time:05-28

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart' hide Router;
import 'package:flutter_scaffold/route/router_handler.dart';

final route = Router();

class Routes {
  static GlobalKey<NavigatorState> navigatorKey = GlobalKey();

  // 路由页面配置
  static String root = "/";
  static String incrementCounter = '/incrementCounter';

  static void configureRoutes(Router router) {
    router.define(root, handler: mainHandler);
    router.define(Routes.incrementCounter, handler: incrementCounterHandler);
  }

when I run flutter run ,the below error occurs enter image description here

I upgraded the fluro ==> fluro: ^2.0.3 for support null-safety

the code can run without errors at before.

if I change import 'package:fluro/fluro.dart'; import 'package:flutter/material.dart' hide Router; to import 'package:fluro/fluro.dart'; import 'package:flutter/material.dart';

IDE can't find Router

enter image description here

enter image description here

CodePudding user response:

Your

import 'package:flutter/material.dart' hide Router;

line says that you intend to import everything except Router from that particular source. So, not seeing Router is expected behavior. You may want to change it to

import 'package:flutter/material.dart';

EDIT

It turned out that Router was renamed to FluroRouter and the code needed to reflect that as well as it needed to be installed. After John ran

flutter pub get

it worked!

CodePudding user response:

Have you tried

final router = FluroRouter();

CodePudding user response:

as mentioned in https://github.com/lukepighetti/fluro/releases the Router class has been removed since version 1.7.0 and replaced by FluroRouter later on

CodePudding user response:

try this:

import 'package:campus/router/i_router.dart';
import 'package:campus/router/not_found_page.dart';
import 'package:campus/screens/home.dart';
import 'package:campus/screens/webview/webview_router.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';

class Routes {
  static String home = '/home';
  
  static final List<IRouterProvider> _listRouter = [];

  static final FluroRouter router = FluroRouter();

  static void initRoutes() {

    /// Specify route jump error return page
    router.notFoundHandler = Handler(
        handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
      debugPrint('init router');
      return const NotFoundPage();
    });

    router.define(home,
        handler: Handler(
            handlerFunc:
                (BuildContext? context, Map<String, List<String>> params) =>
                    const Home()));



    _listRouter.clear();

    _listRouter.add(SettingRouter());
 
    void initRouter(IRouterProvider routerProvider) {
      routerProvider.initRouter(router);
    }

    _listRouter.forEach(initRouter);
  }
}

then implement this

import 'package:fluro/fluro.dart';

abstract class IRouterProvider {
  void initRouter(FluroRouter router);
}

as you see in the first code block in _listRouter.add(SettingRouter()); The respective routes are managed by their respective modules, and the initialization is added here uniformly. to implement module of route use this code :

import 'package:campus/router/i_router.dart';
import 'package:campus/screens/webview/webview.dart';
import 'package:fluro/fluro.dart';


class SettingRouter implements IRouterProvider {
  static String settingPage = '/setting';
  static String settingLanguage = '/setting/language';

  @override
  void initRouter(FluroRouter router) {
    router.define(settingPage,
        transitionType: TransitionType.fadeIn,
        handler: Handler(handlerFunc: (_, __) => const WebView()));
    // router.define(settingLanguage,
    //     handler: Handler(handlerFunc: (_, __) => const LanguageAndLocale()));
     }
}

  • Related