Flutterで端末の情報を取得するには

Flutterで端末の情報を取得するには、device_info_plusプラグインを使用する。

device_info_plusプラグインをインストールする。

flutter pub add device_info_plus

device_info_plusプラグインをインポートする。

import 'package:device_info_plus/device_info_plus.dart';

DeviceInfoPluginのインスタンスを作成する。

static final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();

現在の端末の端末情報を取得する。

final deviceInfo = await deviceInfoPlugin.deviceInfo;

デバイス情報をmapに変換する。

var _data = <String, dynamic>{};
setState(() {
  _data = deviceInfo.toMap();
});

サンプルコード

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  static final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
  var _data = <String, dynamic>{};

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    final deviceInfo = await deviceInfoPlugin.deviceInfo;

    if (!mounted) return;

    setState(() {
      _data = deviceInfo.toMap();
    });
  }

  String _getPlatform() {
    if (kIsWeb) return 'Web';
    if (Platform.isAndroid) return 'Android';
    if (Platform.isIOS) return 'iOS';
    if (Platform.isLinux) return 'Linux';
    if (Platform.isMacOS) return 'MacOS';
    if (Platform.isWindows) return 'Windows';
    return '';
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text(_getPlatform()),
        ),
        body: ListView(
          children: _data.keys.map((String property) {
            return Text('$property : ${_data[property]}');
          }).toList(),
        ),
      ),
    );
  }
}

プラットフォームを判定して、任意の項目の値を取得する。

Future<void> initPlatformState() async {
  final deviceInfo = await deviceInfoPlugin.deviceInfo;
  var data = <String, dynamic>{};

  if (!mounted) return;

  if (kIsWeb) {
    var webBrowserInfo = await deviceInfoPlugin.webBrowserInfo;
    data['userAgent'] = webBrowserInfo.userAgent;
  } else {
    if (Platform.isAndroid) {
      var androidInfo = await deviceInfoPlugin.androidInfo;
      data['device'] = androidInfo.device;
      data['product'] = androidInfo.product;
      data['model'] = androidInfo.model;
      data['version.sdkInt'] = androidInfo.version.sdkInt;
    } else if (Platform.isIOS) {
      var iosInfo = await deviceInfoPlugin.iosInfo;
      data['name'] = iosInfo.name;
      data['model'] = iosInfo.model;
      data['systemName'] = iosInfo.systemName;
      data['systemVersion'] = iosInfo.systemVersion;
    } else if (Platform.isLinux) {
      var linuxInfo = await deviceInfoPlugin.linuxInfo;
      data['name'] = linuxInfo.name;
      data['version'] = linuxInfo.version;
    } else if (Platform.isMacOS) {
      var macOsInfo = await deviceInfoPlugin.macOsInfo;
      data['model'] = macOsInfo.model;
    } else if (Platform.isWindows) {
      var windowsInfo = await deviceInfoPlugin.windowsInfo;
      data['computerName'] = windowsInfo.computerName;
    }
  }

  setState(() {
    _data = data;
  });
}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください