Flutter Get Device MAC Address

前端 未结 3 540
予麋鹿
予麋鹿 2021-01-17 16:50

Does flutter have some code or additional package to get MAC address of the device?

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-17 16:56

    You can Easily get Mac Address in flutter Using the get_mac Package. It supports IOS and Android. Import in Your Dart File Where you want to get Mac Address.

    then Refer to this Example-:

    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    import 'package:get_mac/get_mac.dart';
    
    class SplashScreen extends StatefulWidget {
      @override
      _SplashScreenState createState() => _SplashScreenState();
    }
    
    class _SplashScreenState extends State {
      String _platformID = 'Unknown';
    
      @override
      void initState() {
        super.initState();
        initPlatformState();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.white,
          body: Center(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset(
                  "images/logo.png",
                  height: 250.0,
                  width: 250.0,
                ),
                Text(_platformID)
              ],
            ),
          ),
        );
      }
    
      Future initPlatformState() async {
        String platformVersion;
        try {
          platformVersion = await GetMac.macAddress;
        } on PlatformException {
          platformVersion = 'Failed to get Device MAC Address.';
        }
        print("MAC-: " + platformVersion);
        if (!mounted) return;
        setState(() {
          _platformID = platformVersion;
        });
      }
    }
    

提交回复
热议问题