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
相关产品推荐
相关产品推荐

