feat: add ScannerService with EventChannel
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
27
lib/services/scanner_service.dart
Normal file
27
lib/services/scanner_service.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class ScanResult {
|
||||
final String barcode;
|
||||
final String codeType;
|
||||
|
||||
ScanResult({required this.barcode, required this.codeType});
|
||||
}
|
||||
|
||||
class ScannerService {
|
||||
static const _eventChannel =
|
||||
EventChannel('com.example.pad_scanner/scan');
|
||||
|
||||
Stream<ScanResult>? _scanStream;
|
||||
|
||||
Stream<ScanResult> get scanResults {
|
||||
_scanStream ??= _eventChannel
|
||||
.receiveBroadcastStream()
|
||||
.map((event) => event as Map)
|
||||
.map((event) => ScanResult(
|
||||
barcode: event['barcode'] as String? ?? '',
|
||||
codeType: event['codeType'] as String? ?? 'UNKNOWN',
|
||||
));
|
||||
return _scanStream!;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user