Home > database >  onGenerateRoute: router.generateRoute; Try correcting the prefix or importing the library that defin
onGenerateRoute: router.generateRoute; Try correcting the prefix or importing the library that defin

Time:08-16

Below is the main.dart code. Getting error on generateRoute -> onGenerateRoute: router.generateRoute. Error is

The name 'generateRoute' is being referenced through the prefix 'router', but it isn't defined in any of the libraries imported using that prefix. (Documentation) Try correcting the prefix or importing the library that defines 'generateRoute'. Please help.

import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:initsurvey/core/router.dart' as router;
import 'package:initsurvey/repository/survey/survey_provider.dart';
import 'package:initsurvey/theme/app_theme.dart';
import 'package:provider/provider.dart';

import 'locale/app_translations_delegate.dart';
import 'locale/application.dart';

    void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light
      .copyWith(statusBarIconBrightness: Brightness.light));

  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);

//  ErrorWidget.builder = (FlutterErrorDetails details) => Container(
//    alignment: Alignment.center,
//    child: Icon(Icons.error),
//  );
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  static final GlobalKey<NavigatorState> navKey =  GlobalKey<NavigatorState>();

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  AppTranslationsDelegate _newLocaleDelegate;

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_)=>SurveyProvider(),)
      ],
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        localizationsDelegates: [
          _newLocaleDelegate,
          const AppTranslationsDelegate(),
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        supportedLocales: Application.instance.supportedLocales(),
        navigatorKey: MyApp.navKey,
        title: 'My Flutter App',
        theme: primaryTheme,
        initialRoute: '/',
        onGenerateRoute: router.generateRoute,
      ),
    );
  }

This is the router.dart file code

    class Router {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    /// add settings on MaterialPageRoute for which route you want tracking
    switch (settings.name) {
      case '/':
        return MaterialPageRoute(
            builder: (_) => InitView(), settings: settings);
      case '/login':
        return MaterialPageRoute(
            builder: (_) => LoginView(), settings: settings);
      case '/home':
        return MaterialPageRoute(
            builder: (_) => HomeView(), settings: settings);
      case '/result-view':
        return MaterialPageRoute(
            builder: (_) => ResultView(settings.arguments), settings: settings);
      case '/contact':
        return MaterialPageRoute(
            builder: (_) => ContactView(), settings: settings);
      case '/step-view':
        return MaterialPageRoute(
            builder: (_) => SurveyView(), settings: settings);
      case '/search-address':
        return MaterialPageRoute(
            builder: (_) => SearchAddressView(), settings: settings);
      default:
        return MaterialPageRoute(
            builder: (_) => EmptyView(title: settings.name));
    }
  }

  static String getNameExtractor(RouteSettings settings) {
    /// User for override route's name
    switch (settings.name) {
      case '/':
        return null;
      default:
        return settings.name;
    }
  }
}

CodePudding user response:

There is a typo r will upper case as class name

     onGenerateRoute: Router.generateRoute,

As for the import section as prefix use case will be

import 'package:initsurvey/core/router.dart' as router;
   onGenerateRoute: route.Router.generateRoute,
  • Related