Flutter SDK更新后,经常会出现一些错误。尤其是Flutter 2.0,有些API也跟着变动了,真是牵一发而动全身啊,非常坑。
以下是我更新2.0后老项目的报错解决方法。
错误列表
- 报错信息:Error: No named parameter with the name 'resizeToAvoidBottomPadding'
- BuildContext方法改动
-
- 报错信息:The method ‘ancestorWidgetOfExactType‘ isn‘t defined for the type ‘BuildContext‘
- 报错信息:Error: The method 'ancestorStateOfType' isn't defined for the class 'BuildContext'.
- 报错信息:Error: The method 'inheritFromWidgetOfExactType' isn't defined for the class 'BuildContext'.
- 报错信息:Error: Method not found: 'TypeMatcher'.
- 报错信息:Error: No named parameter with the name 'nullOk'.
- 报错信息:Error: Method not found: 'CupertinoPageRoute.buildPageTransitions'.
- 报错信息:Error: The getter 'title' isn't defined for the class 'TextTheme'
- 报错信息:Error: The getter 'body1' isn't defined for the class 'TextTheme'.
报错信息:Error: No named parameter with the name ‘resizeToAvoidBottomPadding’
原因:resizeToAvoidBottomPadding在新版本的Scaffold中被resizeToAvoidBottomInset属性取代了,替换即可。
Scaffold(
// resizeToAvoidBottomPadding: false,
resizeToAvoidBottomInset: false,
..........
)
BuildContext方法改动
报错信息:The method ‘ancestorWidgetOfExactType‘ isn‘t defined for the type ‘BuildContext‘
原因:BuildContext的ancestorWidgetOfExactType被findAncestorStateOfType替代,并且没有了参数,在泛型中传入需要获取的Widget即可。
ancestorWidgetOfExactType

本文档详细记录了在升级到Flutter 2.0后遇到的常见问题,包括API变动、BuildContext方法调整和废弃类的使用,提供了针对性的修复方法,帮助开发者顺利迁移项目。
最低0.47元/天 解锁文章
3964





