Flutter 每日库: device_info_plus获取设备详细信息
在移动应用开发中,获取设备信息是常见的需求。无论是用于用户行为分析、设备适配优化,还是数据埋点统计,Flutter社区推出的神器device_info_plus都能轻松实现跨平台设备信息获取。本文将通过代码示例和实战数据,带你掌握它的核心用法!
一、快速集成步骤
1. 添加依赖
在 pubspec.yaml 中引入插件:
dependencies:device_info_plus: ^9.0.0
2. 核心代码实现
Future<void> initPlatformState() async {var deviceData = <String, dynamic>{};if (Platform.isIOS) {deviceData = _readIosDeviceInfo(await deviceInfoPlugin.iosInfo);}if (Platform.isAndroid) {deviceData = _readAndroidBuildData(await deviceInfoPlugin.androidInfo);}setState(() {_deviceData = deviceData;});}Map<String, dynamic> _readAndroidBuildData(AndroidDeviceInfo build) {return <String, dynamic>{'version.securityPatch': build.version.securityPatch,'version.sdkInt': build.version.sdkInt,'version.release': build.version.release,'version.previewSdkInt': build.version.previewSdkInt,'version.incremental': build.version.incremental,'version.codename': build.version.codename,'version.baseOS': build.version.baseOS,'board': build.board,'bootloader': build.bootloader,'brand': build.brand,'device': build.device,'display': build.display,'fingerprint': build.fingerprint,'hardware': build.hardware,'host': build.host,'id': build.id,'manufacturer': build.manufacturer,'model': build.model,'product': build.product,'name': build.name,'supported32BitAbis': build.supported32BitAbis,'supported64BitAbis': build.supported64BitAbis,'supportedAbis': build.supportedAbis,'tags': build.tags,'type': build.type,'isPhysicalDevice': build.isPhysicalDevice,'freeDiskSize': build.freeDiskSize,'totalDiskSize': build.totalDiskSize,'systemFeatures': build.systemFeatures,'isLowRamDevice': build.isLowRamDevice,'physicalRamSize': build.physicalRamSize,'availableRamSize': build.availableRamSize,};}Map<String, dynamic> _readIosDeviceInfo(IosDeviceInfo data) {return <String, dynamic>{'name': data.name,'systemName': data.systemName,'systemVersion': data.systemVersion,'model': data.model,'modelName': data.modelName,'localizedModel': data.localizedModel,'identifierForVendor': data.identifierForVendor,'isPhysicalDevice': data.isPhysicalDevice,'isiOSAppOnMac': data.isiOSAppOnMac,'freeDiskSize': data.freeDiskSize,'totalDiskSize': data.totalDiskSize,'physicalRamSize': data.physicalRamSize,'availableRamSize': data.availableRamSize,'utsname.sysname:': data.utsname.sysname,'utsname.nodename:': data.utsname.nodename,'utsname.release:': data.utsname.release,'utsname.version:': data.utsname.version,'utsname.machine:': data.utsname.machine,};}
3.模拟器数据展示
# iOS 数据
flutter: name = iPhone 16
flutter: systemName = iOS
flutter: systemVersion = 18.6
flutter: model = iPhone
flutter: modelName = iPhone 16
flutter: localizedModel = iPhone
flutter: identifierForVendor = B8B20C5F-7C13-4D61-AB90-945A2064A63E
flutter: isPhysicalDevice = false
flutter: isiOSAppOnMac = false
flutter: freeDiskSize = 332186931200
flutter: totalDiskSize = 499963174912
flutter: physicalRamSize = 16384
flutter: availableRamSize = 447
flutter: utsname.sysname: = Darwin
flutter: utsname.nodename: = MacBook-Pro.local
flutter: utsname.release: = 24.6.0
flutter: utsname.version: = Darwin Kernel Version 24.6.0: Mon Aug 11 21:16:05 PDT 2025; root:xnu-11417.140.69.701.11~1/RELEASE_X86_64
flutter: utsname.machine: = iPhone17,3# Android 数据
I/flutter ( 4781): version.securityPatch = 2022-10-01
I/flutter ( 4781): version.sdkInt = 31
I/flutter ( 4781): version.release = 12
I/flutter ( 4781): version.previewSdkInt = 0
I/flutter ( 4781): version.incremental = 10564458
I/flutter ( 4781): version.codename = REL
I/flutter ( 4781): version.baseOS =
I/flutter ( 4781): board = goldfish_x86_64
I/flutter ( 4781): bootloader = unknown
I/flutter ( 4781): brand = google
I/flutter ( 4781): device = emulator64_x86_64_arm64
I/flutter ( 4781): display = sdk_gphone64_x86_64-userdebug 12 SE1A.220826.008 10564458 dev-keys
I/flutter ( 4781): fingerprint = google/sdk_gphone64_x86_64/emulator64_x86_64_arm64:12/SE1A.220826.008/10564458:userdebug/dev-keys
I/flutter ( 4781): hardware = ranchu
I/flutter ( 4781): host = abfarm-release-2004-0082
I/flutter ( 4781): id = SE1A.220826.008
I/flutter ( 4781): manufacturer = Google
I/flutter ( 4781): model = sdk_gphone64_x86_64
I/flutter ( 4781): product = sdk_gphone64_x86_64
I/flutter ( 4781): name = sdk_gphone64_x86_64
I/flutter ( 4781): supported32BitAbis = []
I/flutter ( 4781): supported64BitAbis = [x86_64, arm64-v8a]
I/flutter ( 4781): supportedAbis = [x86_64, arm64-v8a]
I/flutter ( 4781): tags = dev-keys
I/flutter ( 4781): type = userdebug
I/flutter ( 4781): isPhysicalDevice = false
I/flutter ( 4781): freeDiskSize = 5685792768
I/flutter ( 4781): totalDiskSize = 6228115456
I/flutter ( 4781): systemFeatures = [android.hardware.sensor.proximity, android.software.adoptable_storage, android.hardware.sensor.accelerometer, android.software.controls, android.hardware.faketouch, android.software.backup, android.hardware.touchscreen, android.hardware.touchscreen.multitouch, android.software.print, android.software.activities_on_secondary_displays, android.software.voice_recognizers, android.software.picture_in_picture, android.hardware.fingerprint, android.hardware.sensor.gyroscope, android.software.vulkan.deqp.level, android.software.cant_save_state, android.hardware.security.model.compatible, android.hardware.sensor.relative_humidity, android.hardware.bluetooth, android.hardware.camera.autofocus, com.google.android.feature.GOOGLE_BUILD, android.hardware.telephony.gsm, android.hardware.telephony.ims, android.software.incremental_delivery, android.software.opengles.deqp.level, android.hardware.camera.concurrent, android.hardware.audio.output, android.software.verified_boot, android.hardware.camera.flash,
I/flutter ( 4781): isLowRamDevice = false
I/flutter ( 4781): physicalRamSize = 1967
I/flutter ( 4781): availableRamSize = 977
