Event streaming allows your app to receive NFC tag events even when in the foreground without active polling. This is currently Android only.
Implementation Steps
Modify Android MainActivity: Extend FlutterActivity and override onResume, onPause, and onNewIntent. You must use enableForegroundDispatch to capture tags and call FlutterNfcKitPlugin.handleTag(tag) in onNewIntent.
Update AndroidManifest.xml: Ensure your custom MainActivity is set as the main activity.
Listen in Flutter: Use FlutterNfcKit.tagStream.listen((tag) { ... }) to process incoming tags.
WARNING: When using tagStream, DO NOT call FlutterNfcKit.finish().
// 1. Android Kotlin Setup (MainActivity.kt)
class MainActivity : FlutterActivity() {
override fun onResume() {
super.onResume()
val adapter: NfcAdapter? = NfcAdapter.getDefaultAdapter(this)
val pendingIntent: PendingIntent = PendingIntent.getActivity(
this, 0, Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_MUTABLE
)
adapter?.enableForegroundDispatch(this, pendingIntent, null, null)
}
override fun onPause() {
super.onPause()
val adapter: NfcAdapter? = NfcAdapter.getDefaultAdapter(this)
adapter?.disableForegroundDispatch(this)
}
override fun onNewIntent(intent: Intent) {
val tag: Tag? = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
tag?.apply(FlutterNfcKitPlugin::handleTag)
}
}
// 2. Flutter Dart Setup
@override
void initState() {
super.initState();
FlutterNfcKit.tagStream.listen((tag) {
print('Tag detected: ${tag.id}');
// Process tag here
// DO NOT call FlutterNfcKit.finish() in this mode!
});
}