Conditional Compilation Implementation Summary
✅ Successfully Implemented
The Brother Printer functionality now supports conditional compilation, allowing the app to run on iOS Simulator while maintaining full functionality on physical devices.
Architecture
File Structure
lib/utils/
├── brother_printer_util.dart # Main entry point with export control
├── brother_printer_util_device.dart # Full implementation with another_brother SDK
└── brother_printer_util_stub.dart # Stub implementation for unsupported platforms
Implementation Strategy
1. Conditional Export (brother_printer_util.dart)
- Controls which implementation is used
- When
another_brotheris disabled: exports stub for ALL platforms - When
another_brotheris enabled: exports device implementation
2. Device Implementation (brother_printer_util_device.dart)
- Original full-featured implementation
- Requires
another_brotherpackage - Imports Brother SDK classes (Printer, PrinterInfo, Model, etc.)
- Provides real label printing functionality
3. Stub Implementation (brother_printer_util_stub.dart)
- Lightweight replacement when Brother SDK unavailable
- Matches the same public API as device implementation
- Returns error messages: “Brother printer is only supported on physical iOS/Android devices”
- Shows user-friendly snack bars in UI methods
Usage
For iOS Simulator Testing
# 1. Disable Brother printer
./toggle_brother_printer.sh disable
# 2. Clean and rebuild
flutter clean && flutter pub get && cd ios && pod install
# 3. Run on simulator
flutter run --flavor dev -d "iPhone Air"
Result: App runs successfully on simulator with printer features gracefully disabled.
For Physical Device Testing
# 1. Re-enable Brother printer
./toggle_brother_printer.sh enable
# 2. Clean and rebuild
flutter clean && flutter pub get && cd ios && pod install
# 3. Run on device
./run.sh dev ios 00008112-001C20911E68A01E
Result: App runs with full Brother printer functionality.
Benefits
✅ Simulator Testing: Can test all other features without printer hardware ✅ Clean Separation: Device and stub implementations completely isolated ✅ Type Safety: Both implementations export identical public API ✅ User Experience: Graceful fallbacks with informative messages ✅ No Runtime Overhead: Conditional compilation happens at build time ✅ Easy Toggle: Simple script to switch between modes
Testing Checklist
✅ Simulator Build (Verified)
- App compiles without Brother SDK
- App launches on iOS Simulator
- Printer UI shows appropriate messages
- Other features work normally (calendar, check-in, etc.)
🔲 Device Build (To Test)
- App compiles with Brother SDK
- App launches on physical iOS device
- Brother printer discovery works
- Label printing functionality works
- All Brother printer features operational
Technical Notes
Why Toggle Script Is Needed
The another_brother plugin has hard dependencies on Brother SDK native frameworks in its podspec:
s.dependency 'BRLMPrinterKit_AB'
s.dependency 'BROTHERSDK'
These frameworks are device-only (no simulator support). Even with conditional Dart imports, CocoaPods will still try to link the frameworks, causing build failures.
Solution: Temporarily comment out the dependency in pubspec.yaml for simulator builds.
Future Improvements
If Brother releases simulator-compatible frameworks:
- Remove the toggle script requirement
- Update
brother_printer_util.dartto use conditional exports - Use runtime platform detection instead of build-time toggling
Alternative Considered
Weak Linking: Attempted to make frameworks optional via Podfile, but CocoaPods doesn’t support partial weak linking of plugin dependencies.
Files Modified
- ✅
lib/utils/brother_printer_util.dart- Conditional export logic - ✅
lib/utils/brother_printer_util_device.dart- Renamed from original - ✅
lib/utils/brother_printer_util_stub.dart- New stub implementation - ✅
toggle_brother_printer.sh- Helper script to toggle dependency - ✅
docs/BROTHER_PRINTER_SIMULATOR.md- Documentation - ✅
docs/CONDITIONAL_COMPILATION_SUMMARY.md- This file