Flutter auto_route_generator not building, FormatException: Not an instance of List

Viewed 3829

I'm trying to run Flutter pub run build_runner build, to generate a router.g.dart file for navigation.

I've run a build_runner clean, a whole project clean then build but no luck. Im following a tutorial, he just seems to run it exactly with the code I have and its fine. No one else in the comments seem to have this issue and I can't find anything about it online.

It fails saying:

[SEVERE] auto_route_generator:autoRouteGenerator on lib/app/router.dart:

FormatException: Not an instance of List.
[INFO] Running build completed, took 16.2s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 48ms

[SEVERE] Failed after 16.3s
pub finished with exit code 1

My router.dart code:

import 'package:auto_route/auto_route_annotations.dart';
import 'package:stacktest/ui/views/covers/cover_view.dart';
import 'package:stacktest/ui/views/home/home_view.dart';
import 'package:stacktest/ui/views/startup/startup_view.dart';

@MaterialAutoRouter()
class $Router {
  @initial
  StartupView startupViewRoute;
  HomeView homeViewRoute;
  CoverView coverViewRoute;
}

Pubspec.yaml:

  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.3
  stacked: ^1.7.1+1
  auto_route: 0.6.1
  stacked_services: 0.4.4+3

dev_dependencies:
  build_runner: 1.10.0
  auto_route_generator: 0.6.0
  flutter_test:
    sdk: flutter

Flutter doctor -v

✓] Flutter (Channel master, 1.20.0-8.0.pre.41, on Mac OS X 10.15.5 19F101, locale en-AU)
    • Flutter version 1.20.0-8.0.pre.41 at /Users/mikaelwills/Documents/Flutter
    • Framework revision fd80503fd3 (34 hours ago), 2020-07-10 14:41:02 +0530
    • Engine revision 9b3e3410f0
    • Dart version 2.9.0 (build 2.9.0-20.0.dev 06cb010247)

 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/mikaelwills/Library/Android/sdk
    • Platform android-29, build-tools 29.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.5, Build version 11E608c
    • CocoaPods version 1.8.4

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 3.5)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 42.1.1
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] VS Code (version 1.47.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.12.2

[✓] Connected device (2 available)
    • Web Server (web) • web-server • web-javascript • Flutter Tools
    • Chrome (web)     • chrome     • web-javascript • Google Chrome 83.0.4103.116

• No issues found!
1 Answers

Since the version 0.6.0 there were some breaking changes in the auto_route package, the most important was that It was changed the way routes are declared from class fields to a static list. You can check more details about it here.

So, in order to fix it just update it using a static list as follows:

@MaterialAutoRouter(
  routes: <AutoRoute>[
    MaterialRoute(page: StartupView, initial: true),
    MaterialRoute(page: HomeView),
    MaterialRoute(page: CoverView),
  ],
)
class $Router {}

and then run the build runner command: flutter pub run build_runner watch --delete-conflicting-outputs

Another option could be use an earlier version like the 0.5.0, but it's recommended to keep updated, for more info please check the documentation in theauto route package.

Related