可以在扑波中添加元素添加元素

发布于 2025-02-12 09:26:53 字数 600 浏览 0 评论 0原文

如何将元素添加到我的列表decklist中?

decklist.add(first);在红色的红色下,注释: “构造函数的名称必须匹配封闭类的名称。”

import 'dart:ui';

class _GamingCard{
  int ?value1;
  int ?value2;
  String ?name;
  String ?type;
  Image ?image;

  _GamingCard(this.value1,this.value2,this.name,this.type,this.name);
}

class _DeckofCards{
  List<_GamingCard> ?deckList = List<_GamingCard>.empty(growable: true);

  _GamingCard first = _GamingCard(1,10,'ace','heart',null);
  _GamingCard second = _GamingCard(1,10,'ace','cross',null);
  deckList.add(first);
}

how can I add the element first to my List deckList?

deckList.add(first); is under lighted red with the note:
"The name of a constructor must match the name of the enclosing class."

import 'dart:ui';

class _GamingCard{
  int ?value1;
  int ?value2;
  String ?name;
  String ?type;
  Image ?image;

  _GamingCard(this.value1,this.value2,this.name,this.type,this.name);
}

class _DeckofCards{
  List<_GamingCard> ?deckList = List<_GamingCard>.empty(growable: true);

  _GamingCard first = _GamingCard(1,10,'ace','heart',null);
  _GamingCard second = _GamingCard(1,10,'ace','cross',null);
  deckList.add(first);
}

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

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

发布评论

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

评论(1

等待圉鍢 2025-02-19 09:26:53

交战来自零安全性。如果您想首先插入数据,则可以使用构造函数或提供内部列表。

 _DeckofCards() {
    deckList.add(first);
  }

它可以

class _DeckofCards {
  _GamingCard first = _GamingCard(1, 10, 'ace', 'heart', null);
  _GamingCard second = _GamingCard(1, 10, 'ace', 'cross', null);

  late List<_GamingCard> deckList = [first];
}

list在飞镖上

另外一件事,在您的_GamingCard构造函数上,您没有包括图像参数,也最好为此使用命名构造函数。

有关 laster-constructor

The warring is coming from null-safety. If you like to insert data firstly, you can use constructor or provide inside list.

It can be like

 _DeckofCards() {
    deckList.add(first);
  }

Or just add while declaring list

class _DeckofCards {
  _GamingCard first = _GamingCard(1, 10, 'ace', 'heart', null);
  _GamingCard second = _GamingCard(1, 10, 'ace', 'cross', null);

  late List<_GamingCard> deckList = [first];
}

More about List on dart

One more thing, on your _GamingCard constructor, you didn't include image params, also It would be better to use named constructor for this.

More about using-constructors

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