Flutter使用device_info_plus检测iPad Pro(M4)设备识别异常问题
问题:M2/M4芯片iPad Pro被device_info_plus误识别为非iPad设备
使用device_info_plus: ^10.1.0判断设备类型时,在iPad Pro 13英寸(M4)模拟器上,isIpad()函数返回false,原因是info.model字段内容不含"ipad"关键词,导致判断逻辑失效。
解决办法
1. 改用userInterfaceIdiom判断(推荐)
iOS原生提供了userInterfaceIdiom字段来区分设备界面类型,不受芯片型号影响,修改后的判断函数如下:
Future<bool> isIpad() async { print('isIpad Function Called'); DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); IosDeviceInfo info = await deviceInfo.iosInfo; if (info.userInterfaceIdiom == UserInterfaceIdiom.pad) { print('This is iPad'); return true; } else if (info.userInterfaceIdiom == UserInterfaceIdiom.phone) { print('This is iPhone'); return false; } else { print('This is other iOS device or Android'); return false; } }
2. 更新device_info_plus到最新版本
该问题可能是旧版本依赖的兼容性bug,尝试升级到最新版修复:
在pubspec.yaml中修改依赖配置:
device_info_plus: ^latest_version
执行flutter pub get完成依赖更新,新版本大概率已修复M芯片iPad的识别问题。
3. 直接匹配设备型号标识符
如果上述方法无效,可以通过utsname.machine字段获取设备原生型号代码,再进行映射判断:
Future<bool> isIpad() async { DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); IosDeviceInfo info = await deviceInfo.iosInfo; // 包含M系列芯片iPad的型号代码 const ipadMachineCodes = { 'iPad14,12', // M4 iPad Pro 13英寸 'iPad13,1', 'iPad13,2', // M2 iPad Pro 11英寸 'iPad13,4', 'iPad13,5', // M2 iPad Pro 12.9英寸 // 可补充其他型号代码 }; return ipadMachineCodes.contains(info.utsname.machine); }
替代依赖推荐
如果device_info_plus仍无法满足需求,可尝试以下库:
- flutter_device_type:专注设备类型判断,对新设备兼容性更新及时
- device_info:旧版依赖,部分场景下型号识别更精准(注意该库已被弃用,仅作备选)
内容的提问来源于stack exchange,提问作者Jeet Sapovadiya
相关产品推荐
相关产品推荐

