Flutter Stepper控件报错:无法赋值给'Widget Function'参数的解决方法
Flutter Stepper控件controlsBuilder类型错误解决方法
你按照YouTube教程实现Stepper控件时,编写的controlsBuilder代码出现**无法赋值给参数类型'Widget Function'**错误,问题出在函数签名的类型匹配上。
原代码片段:
controlsBuilder: (context, {onStepContinue, onStepCancel}) { final isLastStep = currentStep == getSteps().length - 1; return Container( margin: EdgeInsets.only(top: 50), child: Row( children: [ Expanded( child: ElevatedButton( child: Text(isLastStep ? "Confirm" : "NEXT"), onPressed: onStepContinue, ), ), const SizedBox(width: 12), if (currentStep != 0) Expanded( child: ElevatedButton( child: Text("BACK"), onPressed: onStepCancel, ), ), ], ), ); },
解决方法
Flutter的Stepper.controlsBuilder要求的函数签名中,命名参数onStepContinue和onStepCancel必须显式指定VoidCallback?类型,修改后的代码如下:
controlsBuilder: (BuildContext context, {VoidCallback? onStepContinue, VoidCallback? onStepCancel}) { final isLastStep = currentStep == getSteps().length - 1; return Container( margin: EdgeInsets.only(top: 50), child: Row( children: [ Expanded( child: ElevatedButton( child: Text(isLastStep ? "Confirm" : "NEXT"), onPressed: onStepContinue, ), ), const SizedBox(width: 12), if (currentStep != 0) Expanded( child: ElevatedButton( child: Text("BACK"), onPressed: onStepCancel, ), ), ], ), ); },
原因说明
新版本Flutter对类型推断的要求更严格,未指定类型的命名参数会导致编译器无法匹配controlsBuilder要求的Widget Function(BuildContext, {VoidCallback?, VoidCallback?})类型,显式声明参数类型即可解决类型不匹配问题。
内容的提问来源于stack exchange,提问作者Mario Hooi
相关产品推荐
相关产品推荐

