Flutter项目更新后Windows/Chrome出现Syncfusion组件空安全类型错误
问题描述
项目此前运行正常,Flutter更新后,Windows和Chrome平台均出现4个可空类型不匹配错误,涉及syncfusion_flutter_calendar-20.4.54和syncfusion_flutter_datepicker-20.4.54两个组件。
Windows平台构建错误
构建时出现以下4个类型不匹配错误,最终导致MSB8066构建失败:
../../../Downloads/flutter/.pub-cache/hosted/pub.dartlang.org/syncfusion_flutter_calendar-20.4.54/lib/src/calendar/sfcalendar.dart(10776,71): error G44692867: A value of type 'RenderAbstractViewport?' can't be returned from a function with return type 'RenderAbstractViewport' because 'RenderAbstractViewport?' is nullable and 'RenderAbstractViewport' isn't. [C:\Users\YuriVittori\Documents\MeusProjetos\Project_002_Italian_Citizenship\build\windows\flutter\flutter_assemble.vcxproj]
../../../Downloads/flutter/.pub-cache/hosted/pub.dartlang.org/syncfusion_flutter_datepicker-20.4.54/lib/src/date_picker/date_picker.dart(7596,37): error GC2F972A8: The argument type 'ScrollableState?' can't be assigned to the parameter type 'ScrollableState' because 'ScrollableState?' is nullable and 'ScrollableState' isn't. [C:\Users\YuriVittori\Documents\MeusProjetos\Project_002_Italian_Citizenship\build\windows\flutter\flutter_assemble.vcxproj]
../../../Downloads/flutter/.pub-cache/hosted/pub.dartlang.org/syncfusion_flutter_datepicker-20.4.54/lib/src/date_picker/date_picker.dart(7611,40): error G44692867: A value of type 'ScrollableState?' can't be assigned to a variable of type 'ScrollableState' because 'ScrollableState?' is nullable and 'ScrollableState' isn't. [C:\Users\YuriVittori\Documents\MeusProjetos\Project_002_Italian_Citizenship\build\windows\flutter\flutter_assemble.vcxproj]
../../../Downloads/flutter/.pub-cache/hosted/pub.dartlang.org/syncfusion_flutter_datepicker-20.4.54/lib/src/date_picker/date_picker.dart(7665,71): error G44692867: A value of type 'RenderAbstractViewport?' can't be returned from a function with return type 'RenderAbstractViewport' because 'RenderAbstractViewport?' is nullable and 'RenderAbstractViewport' isn't. [C:\Users\YuriVittori\Documents\MeusProjetos\Project_002_Italian_Citizenship\build\windows\flutter\flutter_assemble.vcxproj]
Chrome平台调试错误
启动调试时出现完全相同的4个错误:
../../../Downloads/flutter/.pub-cache/hosted/pub.dartlang.org/syncfusion_flutter_calendar-20.4.54/lib/src/calendar/sfcalendar.dart:10776:71: Error: A value of type 'RenderAbstractViewport?' can't be returned from a function with return type 'RenderAbstractViewport' because 'RenderAbstractViewport?' is nullable and 'RenderAbstractViewport' isn't.
- 'RenderAbstractViewport' is from 'package:flutter/src/rendering/viewport.dart' ('../../../Downloads/flutter/packages/flutter/lib/src/rendering/viewport.dart').
RenderAbstractViewport get _stackViewPort => RenderAbstractViewport.of(this);
^../../../Downloads/flutter/.pub-cache/hosted/pub.dartlang.org/syncfusion_flutter_datepicker-20.4.54/lib/src/date_picker/date_picker.dart:7596:37: Error: The argument type 'ScrollableState?' can't be assigned to the parameter type 'ScrollableState' because 'ScrollableState?' is nullable and 'ScrollableState' isn't.
- 'ScrollableState' is from 'package:flutter/src/widgets/scrollable.dart' ('../../../Downloads/flutter/packages/flutter/lib/src/widgets/scrollable.dart').
scrollableState: Scrollable.of(context),
^../../../Downloads/flutter/.pub-cache/hosted/pub.dartlang.org/syncfusion_flutter_datepicker-20.4.54/lib/src/date_picker/date_picker.dart:7611:40: Error: A value of type 'ScrollableState?' can't be assigned to a variable of type 'ScrollableState' because 'ScrollableState?' is nullable and 'ScrollableState' isn't.
- 'ScrollableState' is from 'package:flutter/src/widgets/scrollable.dart' ('../../../Downloads/flutter/packages/flutter/lib/src/widgets/scrollable.dart').
..scrollableState = Scrollable.of(context)
^../../../Downloads/flutter/.pub-cache/hosted/pub.dartlang.org/syncfusion_flutter_datepicker-20.4.54/lib/src/date_picker/date_picker.dart:7665:71: Error: A value of type 'RenderAbstractViewport?' can't be returned from a function with return type 'RenderAbstractViewport' because 'RenderAbstractViewport?' is nullable and 'RenderAbstractViewport' isn't.
- 'RenderAbstractViewport' is from 'package:flutter/src/rendering/viewport.dart' ('../../../Downloads/flutter/packages/flutter/lib/src/rendering/viewport.dart').
RenderAbstractViewport get _stackViewPort => RenderAbstractViewport.of(this);
^
解决办法
1. 更新Syncfusion组件到兼容版本
你使用的syncfusion_flutter_calendar和syncfusion_flutter_datepicker版本20.4.54为旧版,不兼容更新后的Flutter空安全API。直接修改pubspec.yaml中组件版本为最新稳定版:
dependencies: syncfusion_flutter_calendar: ^latest_version syncfusion_flutter_datepicker: ^latest_version
执行命令更新依赖:
flutter pub get
Syncfusion后续版本已修复该空类型不匹配问题,这是最优解决方案。
2. 临时源码修改(应急用)
若暂时无法升级组件,可手动修改pub缓存中的源码:
- 定位到
syncfusion_flutter_calendar-20.4.54/lib/src/calendar/sfcalendar.dart第10776行,改为:RenderAbstractViewport get _stackViewPort => RenderAbstractViewport.of(this)!; - 定位到
syncfusion_flutter_datepicker-20.4.54/lib/src/date_picker/date_picker.dart对应行:- 7596行:
scrollableState: Scrollable.of(context)! - 7611行:
..scrollableState = Scrollable.of(context)! - 7665行:
RenderAbstractViewport get _stackViewPort => RenderAbstractViewport.of(this)!;
注意:该修改会在下次flutter pub get时被覆盖,不建议长期使用。
- 7596行:
3. 回退Flutter版本(临时方案)
若不想升级组件,可回退到之前能正常运行的Flutter版本:
flutter downgrade <your_old_flutter_version>
此方式会限制新特性使用,仅作为临时应急方案。
内容的提问来源于stack exchange,提问作者Duas Vias

