An example application to demonstrate NFC in Flutter.
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
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