AgentSkillsCN

flutter-app-troubleshooting

Flutter 移动应用构建与运行时问题调试指南。适用于被要求修复 Flutter 构建错误、运行应用,或排查与后端的连接问题时使用。

SKILL.md
--- frontmatter
name: flutter-app-troubleshooting
description: Guide for debugging Flutter mobile app build and runtime issues. Use this when asked to fix Flutter build errors, run the app, or troubleshoot connection issues with backend.

Flutter App Troubleshooting

Running the Flutter App

CRITICAL: Always run from ecommerce-app/ directory:

powershell
cd ecommerce-app

# Ensure Flutter is in PATH
$env:Path += ";C:\src\flutter\bin"

# Get dependencies first
flutter pub get

# Analyze for issues
flutter analyze

# Run on Chrome (web)
flutter run -d chrome --web-port=3000

# Run on Windows desktop
flutter run -d windows

# Run on Android emulator (if set up)
flutter run -d emulator-5554

Pre-flight Checks

powershell
cd ecommerce-app

# 1. Verify Flutter SDK
flutter --version

# 2. Check available devices
flutter devices

# 3. Verify pubspec.yaml exists
Test-Path "pubspec.yaml"

# 4. Clean build cache (if issues)
flutter clean
flutter pub get

Common Flutter Issues

"flutter: command not found" or "flutter not recognized"

Solution: Add Flutter to PATH

powershell
$env:Path += ";C:\src\flutter\bin"
# Or permanently add C:\src\flutter\bin to system PATH

"No devices found"

Solutions:

  1. For Web: Ensure Chrome is installed
  2. For Windows: Enable Developer Mode in Windows Settings
  3. For Android: Start an emulator first
    powershell
    # List emulators
    flutter emulators
    # Launch emulator
    flutter emulators --launch <emulator_id>
    

"pub get failed" or dependency errors

powershell
cd ecommerce-app
flutter clean
rm pubspec.lock  # If exists
flutter pub get
flutter pub upgrade  # If conflicts

Build errors after code changes

powershell
cd ecommerce-app
flutter clean
flutter pub get
flutter run -d chrome

API Connection Issues

Cannot connect to backend (http://localhost:8080)

Check backend is running first:

powershell
# In separate terminal
cd ecommerce-backend
make run

Verify API endpoint in Flutter:

  • File: lib/core/constants/api_constants.dart
  • Should be: http://localhost:8080/api/v1
  • For web: Use http://localhost:8080 (not http://127.0.0.1)
  • For mobile emulator: Use http://10.0.2.2:8080 (Android) or http://localhost:8080 (iOS)

Test backend manually:

powershell
curl http://localhost:8080/health

CORS errors in web browser

Backend already has CORS configured. If still seeing errors:

  1. Check internal/api/middleware/cors.go in backend
  2. Ensure backend is running
  3. Clear browser cache

State Management Issues (Provider)

"Provider not found" errors

  • Ensure Provider is initialized in lib/main.dart
  • Check if MultiProvider or ChangeNotifierProvider wraps MaterialApp
  • Verify context.read<T>() or context.watch<T>() usage is correct

State not updating

  • Check if notifyListeners() is called in Provider after state changes
  • Verify using context.watch<T>() for reactive updates (not context.read<T>())

Build and Deployment

Building for production

powershell
cd ecommerce-app

# Android APK
flutter build apk --release

# Android App Bundle (for Play Store)
flutter build appbundle --release

# iOS (requires macOS)
flutter build ios --release

# Windows desktop
flutter build windows --release

# Web
flutter build web --release

Output locations:

  • Android: build/app/outputs/flutter-apk/
  • Windows: build/windows/runner/Release/
  • Web: build/web/

Analyzing Code Quality

powershell
cd ecommerce-app

# Run analyzer
flutter analyze

# Format code
flutter format .

# Run tests
flutter test

# Test with coverage
flutter test --coverage

Hot Reload Issues

If hot reload (r) isn't working:

  1. Try hot restart (R in terminal)
  2. Stop app and run again
  3. Clear build cache: flutter clean

Debugging Tips

Enable verbose logging

powershell
flutter run -d chrome --verbose

Inspect widget tree

  • Use Flutter DevTools
  • Run app, then: flutter pub global activate devtools
  • Access at printed URL

Common import errors

  • Ensure all imports use package:ecommerce_app/... format
  • Check name in pubspec.yaml matches
  • Verify file paths are correct

Secure Storage Issues

App uses flutter_secure_storage for tokens.

On Windows: No special setup needed On Android: Handle keystore errors by clearing app data On iOS: Handle keychain access prompts

Clear secure storage for testing

dart
// Add temporarily to code
final storage = FlutterSecureStorage();
await storage.deleteAll();

Performance Optimization

If app is slow:

  1. Run in release mode: flutter run --release
  2. Profile mode: flutter run --profile
  3. Check for unnecessary rebuilds (use const widgets)
  4. Optimize image sizes and assets