如何在 Flutter 中键入 cast multi type ListView items

How do I type cast multi type ListView items in Flutter

提问人:Aaron 提问时间:11/18/2023 最后编辑:Aaron 更新时间:11/18/2023 访问量:28

问:

我有一个 ListView 构建器来构建列表。我希望列表中的第一项与其他列表项一样从不同的列表项构建。

我将第一项添加到数组中:

NoteModel? noteData;
List<NoteModel>? notesData = [];
notesData?.insert(0, widget.note);

我创建了一个动态列表:

List<dynamic>? listData = [];

在 API 响应后,我将项目添加到列表中:

var newList = {...?notesData, ...state.comments}.toList();

在我的列表构建器中,我执行以下操作:

child: ListView.builder(
  itemCount: listData?.length,
  itemBuilder: (context, index) {
    if (index == 0) {
      return NoteBubble(
        notes: listData as List<NoteModel>,
        note: listData![index] as NoteModel,
        index: index,
      );
    }

    return CommentBubble(
      comments: listData as List<CommentModel>,
      comment: listData![index] as CommentModel,
      className: className,
      index: index,
    );
  },
),

在我运行应用程序并抛出错误之前,这不会抱怨。

我试过什么:

我尝试了许多类型的操作,包括我在下面放置的内容。将引发以下错误。它基本上是在抱怨带有“notes: listData as dynamic”的代码行。

type List Object 不是 List NoteModel 类型的子类型

child: ListView.builder(
  itemCount: listData?.length,
  itemBuilder: (context, index) {
    if (index == 0) {
      return NoteBubble(
        notes: listData as dynamic,
        note: listData![index] as dynamic,
        index: index,
      );
    }

    return CommentBubble(
      comments: listData as dynamic,
      comment: listData![index] as dynamic,
      className: className,
      index: index,
    );
  },
),

更新!!!我添加了此代码,因为我能够通过将 List NoteModel 和 List CommentModel 更改为 List dynamic 来使其工作,并且它可以工作。我添加这个是为了防万一有人知道更好的方法,因为这感觉很糟糕

NoteBubble 类

class NoteBubble 扩展 StatelessWidget {

  final Function? onPlay;
  final Function? onPlayNote;

  final List<CommentModel>? comments;
  final List<NoteModel> notes;
  final NoteModel note;
  final int index;

  const NoteBubble({
    super.key,
    this.onPlay,
    this.onPlayNote,
    this.comments,
    required this.notes,
    required this.note,
    required this.index,
  });

CommentBubble 类:

class CommentBubble extends StatelessWidget {
  final String? className;
  final Function? onPlay;
  final bool? player;

  final List<CommentModel> comments;
  final CommentModel comment;
  final int index;

  const CommentBubble({
    super.key,
    this.className,
    this.onPlay,
    this.player,
    required this.comments,
    required this.comment,
    required this.index,
  });
扑动

评论


答:

0赞 Koffi Innocent Konan 11/18/2023 #1

我认为您可以尝试对列表元素的类型进行验证。

ListView.builder(
  itemCount: listData?.length,
  itemBuilder: (context, index) {
    if (listData!.first.runtimeType == NoteModel) { // <= this
      return NoteBubble(
        notes: listData,
        note: listData![index],
        index: index,
      );
    }

    return CommentBubble(
      comments: listData,
      comment: listData![index],
      className: className,
      index: index,
    );
  },
);

评论

0赞 Aaron 11/18/2023
我喜欢你的支票比我的支票 0 更好,但它仍然有同样的抱怨。它似乎真的很讨厌 NoteBubble 小部件中的最终 List<NoteModel> 注释。我真的不喜欢将其更改为动态的解决方案,因为此小部件在其他地方使用