即使值类型没有错误(无效的常数值),也存在错误

发布于 2025-02-12 19:50:55 字数 716 浏览 0 评论 0原文

List<MaterialColor?> changeColor = [
    Colors.red,
    Colors.blue,
    Colors.yellow,
    Colors.green,
    Colors.pink,
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'Mohammed is coming back ',
              style: TextStyle(
                color: changeColor[_counter],
                fontSize: 22,
                fontWeight: FontWeight.bold,
              ),
            ),

我列出了一个由一组颜色组成的列表,我想将其赠送给颜色

List<MaterialColor?> changeColor = [
    Colors.red,
    Colors.blue,
    Colors.yellow,
    Colors.green,
    Colors.pink,
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'Mohammed is coming back ',
              style: TextStyle(
                color: changeColor[_counter],
                fontSize: 22,
                fontWeight: FontWeight.bold,
              ),
            ),

I made a list consisting of a set of colors and I want to give it to the color but it keeps giving me this error, even though I gave the list type the same as the color type

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁绊已千年 2025-02-19 19:50:55

_counter值将更改并进入读取时间。这就是为什么文本小部件不能成为const的原因。从文本中删除const。

body: Center(
  child: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
      Text(
        'Mohammed is coming back ',
        style: TextStyle(
          color: changeColor[_counter],
          fontSize: 22,
          fontWeight: FontWeight.bold,
        ),
      ),
    ],
  ),
),

您可以更多地了解

_counter value will change and get on read time. That's why Text widget can not be const. Remove const from Text.

body: Center(
  child: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
      Text(
        'Mohammed is coming back ',
        style: TextStyle(
          color: changeColor[_counter],
          fontSize: 22,
          fontWeight: FontWeight.bold,
        ),
      ),
    ],
  ),
),

You can more about

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文