VSCode高亮Flutter项目代码错误,Android Studio无此问题
I've run into this exact issue before—VS Code catches Flutter code errors just fine, but Android Studio seems blind to them. Here are the step-by-step fixes that worked for me and other developers:
1. Verify Flutter/Dart Plugins Are Active
First, make sure your IDE has the necessary plugins installed and enabled:
- Open Settings (File > Settings on Windows/Linux; Android Studio > Settings on Mac)
- Navigate to the Plugins section
- Search for "Flutter" and "Dart":
- If either is disabled, check the box next to it and restart Android Studio
- If the plugins are outdated, click Update to get the latest versions, then restart
2. Reconfigure Flutter SDK Path
A misconfigured SDK path often breaks code analysis:
- Go to Settings > Languages & Frameworks > Flutter
- Double-check that the Flutter SDK path points to your actual Flutter root directory (e.g.,
~/flutterorC:\flutter) - If it's wrong, click the
...button to select the correct folder, hit Apply, then restart the IDE
3. Force Cache Invalidation & Restart
Android Studio sometimes gets stuck with stale cache data. A full refresh usually fixes this:
- Click File > Invalidate Caches...
- In the pop-up, check Invalidate and Restart
- Wait for the IDE to reboot and reindex your project—this will trigger a full code analysis
4. Enable Dart/Flutter Code Inspections
Make sure Android Studio is set to check for Flutter/Dart errors:
- Open Settings > Editor > Inspections
- Expand the Dart category and ensure all critical checks (like
Errors,Warnings) are checked - Do the same for the Flutter category
- Click Apply to save changes
5. Refresh Pub Dependencies
Missing or outdated dependencies can throw off code highlighting:
- Open the terminal at the bottom of Android Studio and run:
flutter pub get - After it finishes, click the Sync Project with Gradle Files button (top-right corner) or go to Tools > Flutter > Flutter Pub Get
- Wait for the sync to complete, then check your code again
6. Confirm Dart SDK is Linked to Flutter
Ensure Android Studio uses the Dart SDK bundled with Flutter (not a standalone one):
- Go to Settings > Languages & Frameworks > Dart
- Check Enable Dart support for the project [your-project-name]
- For Dart SDK path, select the
dart-sdkfolder inside your Flutter SDK:[flutter-path]/bin/cache/dart-sdk - Hit Apply and restart the IDE
If none of these work, try creating a brand new Flutter project in Android Studio. If the new project highlights errors correctly, your original project might have corrupted config files—you can try copying your code into the new project to fix it.
内容的提问来源于stack exchange,提问作者Igor

