DEV Community

Cover image for Writing and Reading NFC in Flutter | Comprehensive Guide
Anshi
Anshi

Posted on

Writing and Reading NFC in Flutter | Comprehensive Guide

Near Field Communication (NFC) technology has transformed the way devices interact, enabling seamless data exchange with just a tap. Whether you're building an access control system, contactless payment app, or smart inventory tracking, NFC integration in Flutter can open up new possibilities.

In this guide, we'll cover everything from setting up NFC in Flutter to reading and writing NFC tags efficiently.

What is NFC and Why Use It?

NFC is a short-range wireless communication technology that allows devices to interact when they are within close proximity. Common applications of NFC include:

  • Contactless payments (Google Pay, Apple Pay)
  • Access control systems (keyless entry)
  • Data sharing and pairing (Bluetooth, Wi-Fi)
  • Smart advertising (interactive posters)
  • Inventory and asset tracking (scanning tags for logistics)

Flutter provides robust support for NFC through third-party packages, making implementation seamless.

Setting Up NFC in Flutter

Before diving into implementation, ensure you have the following prerequisites:

  • A Flutter project set up
  • A smartphone with NFC capabilities
  • Dependencies for handling NFC in Flutter

Installing Dependencies

The most widely used Flutter package for NFC functionality is flutter_nfc_kit.

Install it by running:

flutter pub add flutter_nfc_kit
Enter fullscreen mode Exit fullscreen mode

Configuring Permissions

For Android, update AndroidManifest.xml with the following permissions:

<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
Enter fullscreen mode Exit fullscreen mode

For iOS, add the following key to Info.plist:

<key>NFCReaderUsageDescription</key>
<string>We need NFC access to read and write tags.</string>
Enter fullscreen mode Exit fullscreen mode

Reading NFC Tags in Flutter

Once NFC is set up, you can start reading NFC tags. Use the following snippet to detect and read NFC tags:

import 'package:flutter_nfc_kit/flutter_nfc_kit.dart';

Future<void> readNFCTag() async {
  try {
    NFCTag tag = await FlutterNfcKit.poll();
    print('NFC Tag Found: ${tag.id}');
  } catch (e) {
    print('Error reading NFC tag: $e');
  }
}

Enter fullscreen mode Exit fullscreen mode

Call readNFCTag() when you want to scan an NFC tag.

Writing Data to NFC Tags

Writing to NFC tags is straightforward. Use the writeNDEFRecords method as shown below:

import 'package:flutter_nfc_kit/flutter_nfc_kit.dart';

Future<void> writeNFCTag(String message) async {
  try {
    NFCTag tag = await FlutterNfcKit.poll();
    await FlutterNfcKit.writeNDEFRecords([NDEFRecord.text(message)]);
    print('Successfully wrote to NFC tag');
  } catch (e) {
    print('Error writing to NFC tag: $e');
  }
}

Enter fullscreen mode Exit fullscreen mode

Handling NFC Events and Errors

It’s crucial to handle errors and user interactions properly. Here are some common issues and solutions:

  • NFC not supported – Check FlutterNfcKit.supported() before initializing NFC features.
  • Tag polling timeout – Display a UI message instructing users to tap their device on the tag properly.
  • Permission issues – Ensure the required permissions are added in AndroidManifest.xml and Info.plist.

Use Cases for NFC in Flutter Apps

  • Attendance tracking – Use NFC tags for employee check-in systems.
  • Event ticketing – Store ticket information in NFC tags for quick verification.
  • Smart business cards – Embed contact details in NFC tags for seamless sharing.
  • IoT device pairing – Tap an NFC tag to instantly connect a device to a Wi-Fi network.

Final Thoughts

NFC technology in Flutter unlocks exciting opportunities for mobile app development. By following this guide, you can implement seamless NFC reading and writing in your apps, enhancing user experience and functionality.

For more insights into mobile app development, check out our blog on NFC technology in Flutter!

Top comments (0)