13

I am facing this issue when I run flutter pub run build_runner build. I have tried solutions like flutter clean, flutter pub-cache repair flutter pub get, etc. Dependencies like Mockito, HTTP, and build_runner are added on pubspec and flutter pub get ran successfully as well.

Failed to precompile build_runner:build_runner:
../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-1.6.0/lib/src/error/best_practices_verifier.dart:1600:40: Error: Getter not found: 'topLevelVariable'.
      return kinds.contains(TargetKind.topLevelVariable);
                                       ^^^^^^^^^^^^^^^^
../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-1.6.0/lib/src/error/best_practices_verifier.dart:2024:23: Error: Getter not found: 'topLevelVariable'.
      case TargetKind.topLevelVariable:
                      ^^^^^^^^^^^^^^^^
../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-1.6.0/lib/src/error/best_practices_verifier.dart:2024:23: Error: Type 'dynamic' of the case expression is not a subtype of type 'TargetKind' of this switch expression.
 - 'TargetKind' is from 'package:meta/meta_meta.dart' ('../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/meta-1.3.0/lib/meta_meta.dart').
      case TargetKind.topLevelVariable:
                      ^
../../snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-1.6.0/lib/src/error/best_practices_verifier.dart:2001:13: Context: The switch expression is here.
    switch (this) {
            ^
pub finished with exit code 1
Hamed
  • 5,867
  • 4
  • 32
  • 56
Hassan
  • 151
  • 1
  • 8

8 Answers8

13

adding the analyser https://pub.dev/packages/analyzer dependency 1.5.0 in the dev-dependencies of pubspec yaml worked for me

 dev_dependencies:
   analyzer: 1.5.0
Raj
  • 131
  • 3
5

As stated here: https://github.com/dart-lang/sdk/issues/46136#issuecomment-848910733, you should be able to just run flutter pub upgrade without specifying any analyzer version, and it'll work!

Chris Moore
  • 113
  • 1
  • 8
3

Try add these lines to your pubspec.yaml

dependency_overrides:
   meta: 1.6.0
gilbriatore
  • 658
  • 7
  • 12
  • Thank you. I didn't wanted to upgrade to latest version for the moment. This is a good solution for anyone who don't want to upgrade to latest versions for the moment. – Hiran Walawage Dec 20 '21 at 16:17
2
flutter pub upgrade --major-versions

This will do the thing. Happy coding.

Jashan PJ
  • 4,177
  • 4
  • 27
  • 41
1

dependency_overrides: analyzer: '1.5.0'

Adding this pubspec.yaml file worked for me

Hassan
  • 151
  • 1
  • 8
1

it seems to be an issue with newest verson of analyser. I was able to fix it by just downgrading the version from 1.6.0 to 1.5.0.

dev_dependencies: analyzer: 1.5.0

Ankit Sharma
  • 11
  • 1
  • 1
0

In my case build runner, I have upgraded build runner and few other dependency.

Build runner for generating part files

build_runner: ^2.1.2

0

Update dependencies to latest versions. It's works for me.

auto_route:
build_runner:
auto_route_generator:
ziqq
  • 523
  • 5
  • 11