flutter nfc plugin only scans once

1,065

I have similar problem with this code (correct me if I'm wrong but it seems to be sample from package description)). In my case I can read many tags after starting an app and after starting reading tags (Start NFC button). But after pressing Stop NFC button and again Start NFC button, app doesn't read and doesn't show tags. I think the tags are read by nfc module (Samsung A5 in my case, I can hear the same sound) but app form (page) stays the same - no tag info (null only). When I closed an app and started it again tags were read again. I tried to change a little startNFC() method which is like this in my case:

Future<void> startNFC() async {
    NfcData response;

    setState(() {
      _nfcData = NfcData();
      _nfcData.status = NFCStatus.reading;
    });

    print('NFC: Scan started');

    try {
      print('NFC: Scan readed NFC tag');
       Stream<NfcData> res = await FlutterNfcReader.read;
       if (res != null) { 
         response = await res.first;
       }
    } on PlatformException {
      print('NFC: Scan stopped exception');
    }
    setState(() {
      _nfcData = response;
    });
  }

Now, I can read tag once after tap Start NFC button. If I tap Stop NFC button and again Start NFC button everything works fine (one read). Of course you can call startNFC() method programaticaly and controll this way entire read process. Good luck!

Share:
1,065
Killian Byrne
Author by

Killian Byrne

Updated on December 09, 2022

Comments

  • Killian Byrne
    Killian Byrne over 1 year

    I'm new to flutter. I'm using this nfc plugin to read an nfc tag in an android app. Currently, when i open my app, i can scan a tag once. But if I try to scan more than one tag, nothing is read. Here is the sample code I have used from the plugin example. Any help would be much appreciated