Flutter运行报错:http包中'dothrow'不是有效类型
解决Flutter运行时http包编译错误
错误核心原因
问题出在http 0.13.6版本与当前Dart/Flutter版本不兼容,具体冲突点:
- 老版本http包使用了已废弃的
dothrow语法(正确写法应为throw) - 代码依赖的
records特性需要Dart 3.0+,但该版本http包的语言版本限制较低 - 非空返回值的逻辑不符合当前Dart的空安全规范
解决步骤
升级http包到兼容版本
打开项目根目录的pubspec.yaml,将http依赖项更新为最新稳定版(示例为^1.1.0,可根据实际情况选更高版本):dependencies: http: ^1.1.0执行命令更新依赖:
flutter pub get清理缓存并重新构建
先清理旧编译缓存:flutter clean重新获取依赖后运行项目:
flutter pub get flutter run检查并升级Flutter/Dart版本(可选)
若升级http包后仍报错,确认Flutter版本是否支持Dart 3.0+:flutter --version版本过低则执行升级:
flutter upgrade
原始错误日志
../../../.pub-cache/hosted/pub.dev/http-0.13.6/lib/src/io_client.dart:94:8: Error: 'dothrow' isn't a type. dothrow _ClientSocketException(error, request.url); ^^^^^^^ ../../../.pub-cache/hosted/pub.dev/http-0.13.6/lib/src/io_client.dart:94:16: Error: Expected ';' after this. dothrow _ClientSocketException(error, request.url); ^^^^^^^^^^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/http-0.13.6/lib/src/io_client.dart:94:38: Error: The 'records' language feature is disabled for this library. Try removing the package language version or setting the language version to 3.0 or higher. dothrow _ClientSocketException(error, request.url); ^ ../../../.pub-cache/hosted/pub.dev/http-0.13.6/lib/src/io_client.dart:54:30: Error: A non-null value must be returned since the return type 'IOStreamedResponse' doesn't allow null. - 'IOStreamedResponse' is from 'package:http/src/io_streamed_response.dart' ('../../../.pub-cache/hosted/pub.dev/http-0.13.6/lib/src/io_streamed_response.dart'). Future<IOStreamedResponse> send(BaseRequest request) async { ^ Target kernel_snapshot failed: Exception FAILURE: Build failed with an exception. * Where: Script '/home/antonio.daniel/Dev/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 1159 * What went wrong: Execution failed for task ':app:compileFlutterBuildDebug'. > Process 'command '/home/antonio.daniel/Dev/flutter/bin/flutter'' finished with non-zero exit value 1 * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 1m 25s Running Gradle task 'assembleDebug'... 86.5s Exception: Gradle task assembleDebug failed with exit code 1
内容的提问来源于stack exchange,提问作者A C
相关产品推荐
相关产品推荐

