flutterdartrouterauto-route

Flutter auto-router can not created by command


flutter part 'app_router.gr.dart' not creating with command. should i create myself manually

I'm currently using Mac and i export path but now working. I tried
flutter pub run build_runner build --delete-conflicting-outputs command but it is not create the file

import 'package:auto_route/auto_route.dart';
import 'package:weather_app/ui/screens/home_screen.dart';

@MaterialAutoRouter(
  routes: <AutoRoute>[
    MaterialRoute(page: HomeScreen, initial: true)

  ],
)
class $AppRouter {}

import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:weather_app/core/di/service_locator.dart';
import 'app_router.gr.dart';

void main() {
  setupLocator();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  final _appRouter = AppRouter();

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerDelegate: AutoRouterDelegate(
        _appRouter,
        navigatorObservers: () => [AutoRouteObserver()],
      ),
      routeInformationParser: _appRouter.defaultRouteParser(),
    );
  }
}

Solution

  • Check your auto_route version and @MaterialAutoRouter is deprecated. See: migration-to-v6

    Updated:

    import 'package:auto_route/auto_route.dart';
    import 'package:de_weather/src/presentation/screens/home/home_screen.dart';
    
    
    part 'app_router.gr.dart';
    
    @AutoRouterConfig()
    class AppRouter extends _$AppRouter {
    
    //Default route type 
      @override
     RouteType get defaultRouteType => const RouteType.material(); //.cupertino, .adaptive
    
      @override
      List<AutoRoute> get routes => [
            /// routes go here
            AutoRoute(page: HomeScreen.page)
          ];
    }
    

    Add @RoutePage annotation on you HomeScreen class.

    @RoutePage()
    class HomeScreen extends StatefulWidget {
      const HomeScreen({super.key});
    
      @override
      State<HomeScreen> createState() => _HomeScreenState();
    }
    
    class _HomeScreenState extends State<HomeScreen> {
      @override
      Widget build(BuildContext context) {
        return const Placeholder();
      }
    }
    

    and run command

    flutter pub run build_runner build --delete-conflicting-outputs
    

    main.dart

    
    void main() async {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      MyApp({super.key});
    
      final _appRouter = AppRouter();
    
      @override
      Widget build(BuildContext context) {
    
        return MaterialApp.router(
          debugShowCheckedModeBanner: false,
          theme: AppTheme.light,
          routerConfig: _appRouter.config(),
        );
      }
    }