求助:Flutter中Widget无法赋值给InheritedWidget类型变量错误
Flutter构建错误解决:Widget无法赋值给InheritedWidget类型
我并非Flutter开发者,在构建项目时遇到以下错误:
错误信息:
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/syncfusion_flutter_gauges-19.4.56/lib/src/linear_gauge/gauge/linear_gauge_scope.dart:40:10: Error: A value of type 'Widget' can't be assigned to a variable of type 'InheritedWidget'. - 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/framework.dart'). - 'InheritedWidget' is from 'package:flutter/src/widgets/framework.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/framework.dart'). .widget; ^ FAILURE: Build failed with an exception. * Where: Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1159 * What went wrong: Execution failed for task ':app:compileFlutterBuildDebug'. > Process 'command 'C:\src\flutter\bin\flutter.bat'' 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 26s Exception: Gradle task assembleDebug failed with exit code 1
错误原因
你使用的syncfusion_flutter_gauges版本(19.4.56)过旧,与当前Flutter版本不兼容。旧版本包在获取InheritedWidget时未做类型转换,而新版本Flutter中相关API返回类型调整为Widget,导致类型不匹配。
解决步骤
- 打开项目根目录下的
pubspec.yaml文件,找到syncfusion_flutter_gauges依赖项,将版本号更新为最新兼容版本(例如^25.1.35,可根据官方最新版本调整) - 在项目根目录打开终端,执行以下命令清理缓存并重新拉取依赖:
flutter clean flutter pub get - 重新执行构建命令:
flutter run
内容的提问来源于stack exchange,提问作者Md Siyam
相关产品推荐
相关产品推荐

