Flutter使用internet_speed包遇MissingPluginException及Socket关闭错误求解决
Flutter internet_speed 包报错问题解决
遇到的错误
启动测速时触发两类核心错误:
MissingPluginException(No implementation found for method startListening on channel internet_speed)- 日志反复输出
CONNECTION_ERROR、Socket is closed
错误日志:
I/System.out(12436): port:80 I/flutter (12436): the errorMessage CONNECTION_ERROR, the speedTestError Socket is closed I/flutter (12436): the errorMessage CONNECTION_ERROR, the speedTestError Socket is closed W/1.raster(12436): type=1400 audit(0.0:100191): avc: denied { search } for name="battery" dev="sysfs" ino=6688 scontext=u:r:untrusted_app:s0:c170,c257,c512,c768 tcontext=u:object_r:sysfs_batteryinfo:s0 tclass=dir permissive=0 I/flutter (12436): the errorMessage CONNECTION_ERROR, the speedTestError Socket is closed
最小复现代码
class SpeedTestPage extends StatefulWidget { const SpeedTestPage({Key? key}) : super(key: key); @override State createState() => _SpeedTestPageState(); } class _SpeedTestPageState extends State { InternetSpeed internetSpeed = InternetSpeed(); @override Widget build(BuildContext context) { return InkWell( child: Text("Click to start"), onTap: () { internetSpeed.startDownloadTesting( onDone: (double transferRate, SpeedUnit unit) { debugPrint('the transfer rate $transferRate, the percent 100'); }, onProgress: (double percent, double transferRate, SpeedUnit unit) { debugPrint('the transfer rate $transferRate, the percent $percent'); }, onError: (String errorMessage, String speedTestError) { debugPrint('the errorMessage $errorMessage, the speedTestError $speedTestError'); }, ); }, ); } }
解决办法
针对MissingPluginException
- 执行命令清理缓存并重新获取依赖:
flutter clean flutter pub get - 重启IDE,彻底终止当前App进程后重新运行
- 检查AndroidManifest.xml,确保添加必要网络权限:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> - 确认插件版本,尝试升级到最新稳定版,或回退到已知可用的旧版本
针对CONNECTION_ERROR与Socket is closed
- 手动指定稳定的测速服务器,替换默认服务器:
internetSpeed.startDownloadTesting( testServer: "http://cdn.example.com", // 替换为稳定的大文件服务器地址 // 其他回调参数... ); - 检查设备网络状态,确保网络连接稳定,排除弱网、VPN或代理干扰
- 添加点击防抖,避免频繁触发测速导致连接冲突:
bool _isTesting = false; // ... onTap: () { if (_isTesting) return; setState(() => _isTesting = true); internetSpeed.startDownloadTesting( // 回调参数... onDone: (rate, unit) { setState(() => _isTesting = false); // 其他逻辑 }, onError: (msg, err) { setState(() => _isTesting = false); // 错误处理 }, ); }, - 排除设备防火墙或安全软件对App网络请求的拦截
代码修正提示
原代码存在几个语法问题,建议修正:
State需指定泛型:State<_SpeedTestPageState>@OverRide应为小写的@overridebuild方法需返回完整Widget,比如用Scaffold包裹:@override Widget build(BuildContext context) { return Scaffold( body: Center( child: InkWell( // 原代码内容... ), ), ); }
内容的提问来源于stack exchange,提问作者Moses
相关产品推荐
相关产品推荐

