You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase获取歌曲列表后打印显示Instance of SongEntity问题求助

解决控制台打印SongEntity列表显示[Instance of 'SongEntity']的问题

这是因为Dart中默认打印类实例时,只会输出Instance of '类名',想要看到具体属性内容,只需要在SongEntity类里重写toString()方法就行。

  • 步骤1:在你的SongEntity类中添加toString()方法,把需要展示的属性都包含进去
    示例代码:

    class SongEntity {
      final String id;
      final String title;
      final String artist;
      // 其他属性...
    
      SongEntity({required this.id, required this.title, required this.artist});
    
      // 重写toString方法
      @override
      String toString() {
        return 'SongEntity(id: $id, title: $title, artist: $artist)';
      }
    }
    
  • 步骤2:修改后重新运行代码,再打印songs列表,就能看到每个SongEntity实例的具体属性值了

如果需要更结构化的输出(比如JSON格式),也可以结合dart:convert包的jsonEncode方法,示例:

import 'dart:convert';

// 打印时使用
print(jsonEncode(songs.map((song) => {
  'id': song.id,
  'title': song.title,
  'artist': song.artist
}).toList()));

内容的提问来源于stack exchange,提问作者Teewhy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 16:47:01