Open In App

How to Get MAC Address of Device in Flutter?

Improve
Improve
Like Article
Like
Save
Share
Report

Flutter SDK is an open-source software development kit by Google to develop applications for multiple platforms from a single codebase. Sometimes, your app needs to know the MAC address of the device. MAC (Media Access Control) address is the unique identifier of a device on a LAN network.

Approach: Use the flutter package get_mac to get the MAC address of the client’s device.

Step by Step Implementation

Step 1: Navigate to pubspec.yaml file

Open your project in VS Code and navigate to pubspec.yaml file:

Step 2: Add the dependency

Add the get_mac dart package as the dependency and save the file.

Step 3: Download the dependencies

Open the terminal in VS code and run the below command to download the added dependencies.

flutter pub get

Step 4: Example Code

We will use the macAddress property of GetMac to get the MAC address of the device. This works with both iOS and Android devices.

Syntax:

String macAddress = await GetMac.macAddress;

Dart




import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get_mac/get_mac.dart';
  
void main() {
  runApp(const MyApp());
}
  
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MAC Finder',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: const Home(),
    );
  }
}
  
class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);
  
  @override
  State<Home> createState() => _HomeState();
}
  
class _HomeState extends State<Home> {
  String _deviceMAC = 'Click the button.';
  // Platform messages are async in nature
  // that's why we made a async function.
  Future<void> initMacAddress() async {
    String macAddress;
  
    try {
      macAddress = await GetMac.macAddress;
    } on PlatformException {
      macAddress = 'Error getting the MAC address.';
    }
  
    setState(() {
      _deviceMAC = macAddress;
    });
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('MAC address of a device'),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              _deviceMAC,
              style: const TextStyle(fontSize: 26, fontWeight: FontWeight.bold),
            ),
            ElevatedButton(
              onPressed: () {
                initMacAddress();
              },
              child: const Text("Get MAC Address"),
            ),
          ],
        ),
      ),
    );
  }
}


Output:

Output

Note: The get_mc package does not support null safety. You may have to use: flutter run –no-sound-null-safety to run the app.



Last Updated : 20 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads