Flutter String 错误声明了 headerLabel 但未找到

问题描述 投票:0回答:1

找不到 headerLabel 来使用它。尝试使用变量 headerLabel 但找不到解决方案

class ProfileHeaderLabel extends StatelessWidget {
  final String headerLabel;
  const ProfileHeaderLabel({
   Key? key, required this.headerLabel
  }): super(key: key)

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 40,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: const [
          SizedBox(
            height: 40,
            width: 50,
            child: Divider(
              color: Colors.grey,
              thickness: 1,
            ),
          ),
          // 'Account info'
          Text(
            headerLabel,
            style: TextStyle(
                color: Colors.grey, fontSize: 24, fontWeight: FontWeight.w600),
          ),

找不到 headerLabel 来使用它。

flutter
1个回答
1
投票

在您的代码中:

Widget build(BuildContext context) {
    return SizedBox(
        height: 40,
        child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: const [ // --> This line
        SizedBox(

您正在尝试在常量 (

headerLabel
) 内使用非常量表达式 (
const [...]
)。

但在您的情况下, headerLabel 不是常量,因为它的值是在运行时确定的,而不是在编译时确定的。

要解决此问题,只需从 Row 的子级中删除

const
即可:

 Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [ // Remove `const`
        SizedBox(

另请参阅:

© www.soinside.com 2019 - 2024. All rights reserved.