Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conflict between json_serializable and auto_route during build_runner execution #1391

Open
terisuke opened this issue Jan 20, 2024 · 1 comment

Comments

@terisuke
Copy link

Hello,

I'm encountering an issue when using both json_serializable and auto_route in my Flutter project. When I run flutter pub run build_runner build --delete-conflicting-outputs, I receive the following error:

[SEVERE] json_serializable on lib/config/routes/auto_router.dart (cached):

line 1, column 431 of package:flutter_news/config/routes/auto_router.dart: Could not resolve annotation for class $AppRouter.
╷
1 │ ┌ @MaterialAutoRouter(
2 │ │ replaceInRouteName: 'Page,Route',
3 │ │ routes: <AutoRoute>[
4 │ │ AutoRoute(
5 │ │ path: '/',
6 │ │ page: MainPage,
7 │ │ children: [
8 │ │ AutoRoute(
9 │ │ path: 'news_headline',
10 │ │ page: NewsHeadlinePage,
11 │ │ initial: true,
12 │ │ ),
13 │ │ AutoRoute(path: 'news_search', page: NewsSearchPage),
14 │ │ AutoRoute(path: 'setting', page: SettingPage),
15 │ │ ],
16 │ │ ),
17 │ │ AutoRoute(path: '/news_detail', page: NewsDetailPage),
18 │ │ ],
19 │ └ )
╵
[SEVERE] Failed after 138ms

It seems that json_serializable is trying to parse the $AppRouter class, which is generated by auto_route, and this is causing the error. I've tried excluding the auto_router.dart file from analysis in analysis_options.yaml, but this doesn't seem to affect build_runner or json_serializable.

Here is the code for my auto_router.dart file:

import 'package:auto_route/auto_route.dart';
import 'package:flutter_news/ui/main/main_page.dart';
import 'package:flutter_news/ui/news_detail/news_detail.dart';
import 'package:flutter_news/ui/news_headline/news_headline_page.dart';
import 'package:flutter_news/ui/news_search/news_search_page.dart';
import 'package:flutter_news/ui/setting/setting_page.dart';

part 'app_router.gr.dart';

@MaterialAutoRouter(
replaceInRouteName: 'Page,Route',
routes: <AutoRoute>[
AutoRoute(
path: '/',
page: MainPage,
children: [
AutoRoute(
path: 'news_headline',
page: NewsHeadlinePage,
initial: true,
),
AutoRoute(path: 'news_search', page: NewsSearchPage),
AutoRoute(path: 'setting', page: SettingPage),
],
),
AutoRoute(path: '/news_detail', page: NewsDetailPage),
],
)
class $AppRouter {}

In addition to the above information, I am currently using Flutter SDK version 3.16.8 and Dart SDK version 3.2.5.
This is the repository in question:
https://github.com/terisuke/flutter_news

I would appreciate any guidance on how to resolve this issue. Thank you.

@gaetanora
Copy link

Hi, how did you solve it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants