Skip to content

futuremaker-ma/nfc_example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NFC in Flutter example

An example application to demonstrate NFC in Flutter.

Screenshot of the example app

It is easiest to run the app on an Android device as the iOS simulator can't do NFC and running it on a real device requires a bunch of code signing nonsense.

$ flutter run

Implementation

The app has a button in the top right corner that starts or stops listening for NFC tags.

RaisedButton(
    child: Text(reading ? "Stop reading" : "Start reading"),
    onPressed: () {
        if (!reading) {
            setState(() {
                reading = true;
                session = NFC.readNDEF()
                    .listen((tag) {

                    });
            });
        } else {
            session?.cancel();
            setState(() {
                reading = false;
            });
        }
    }
);

When a tag is scanned, it inserts it in a list of scanned tags, which is then rendered in a ListView.

NFC.readNDEF()
    .listen((tag) (
        setState(() {
            tags.insert(0, tag);
        });
    ));

When an error occurs it will show an AlertDialog, unless the error is a NFCUserCanceledSessionException.

NFC.readNDEF()
    .listen((tag) {
        // ...
    }, onError: (error) {
        if (!(error is NFCUserCanceledSessionException)) {
            // It is up to you how many exceptions you want to check for.
            showDialog(
                context: context,
                builder: (context) => AlertDialog(
                    title: const Text("Error!"),
                    content: Text(e.toString()),
                ),
            );
        }
    });
```# nfc_example

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published