google / json_serializable.dart

Generates utilities to aid in serializing to/from JSON.
https://pub.dev/packages/json_serializable
BSD 3-Clause "New" or "Revised" License
1.54k stars 392 forks source link

Conflict between json_serializable and auto_route during build_runner execution #1391

Open terisuke opened 5 months ago

terisuke commented 5 months ago

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 commented 3 months ago

Hi, how did you solve it?