google / google/json_serializable.dart
Conflict between json_serializable and auto_route during build_runner execution
- Dominant language
- Dart
- Stars
- 1.6k
- Forks
- 461
- Avg merge
- 45m
- Merged PRs (30d)
- 1
Description
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: [
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:
```lib/config/routes/auto_router.dart
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(
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.
Contributor guide
Assessment
This issue has not been assessed yet.