我知道这个错误是什么,但无法确定是代码的哪一部分导致的。
我如何识别和理解我的代码的哪一部分导致了这种情况?
我已经创建了一些非常基本的单元测试来验证我的fromJson/jsonDecode和toJson/JasonEncode函数,除了在使用嵌套类/列表时使用fromJson之外,其他一切都可以正常工作。
当使用toJson/jsonEncode时,我能够成功地生成以下字符串
{"id":1,"title":"The Hits","songList":[{"id":1,"title":"hello","artist":"me","lyrics":"say stuff","genre":"rock","rank":10,"created":"2020-04-06T22:00:00.000"},{"id":2,"title":"howdy","artist":"you","lyrics":"say no","genre":"hillBilly","rank":9,"created":"2019-04-06T22:00:00.000"},{"id":3,"title":"Ola","artist":"we","lyrics":"say nothing","genre":"Flapp","rank":2,"created":"2018-04-06T22:00:00.000"}],"created":"2018-04-06T22:00:00.000"}
我不明白为什么fromJson/Dedecode由于强制转换错误而失败。
单元测试
test('Create Setlist including songList from complex json', (){
Setlist complexSet = Setlist.fromJson(jsonDecode(complexObjectJson));
print(complexSet);
});
这是来自Setlist类的Setlist.fromJson工厂方法
factory Setlist.fromJson(dynamic json) {
if (json['songList'] != null) {
var songObjsJson = json['songList'] as List;
List<Song> _songs = songObjsJson.map((songJson) => Song.fromJson(songJson)).toList();
return Setlist(
id: json['id'] as int,
title: json['title'] as String,
songList: _songs,
created: ['created'] as String,
);
} else {
return Setlist(
id: json['id'] as int,
title: json['title'] as String,
created: ['created'] as String,
);
}
}
歌曲类
class Song {
final int id;
final String title;
final String artist;
final String lyrics;
final String genre;
final int rank;
final String created;
Song(
{this.id,
this.title,
this.artist,
this.lyrics,
this.genre,
this.rank,
this.created});
Map toJson() => {
'id': id,
'title': title,
'artist': artist,
'lyrics': lyrics,
'genre': genre,
'rank': rank,
'created': created,
};
factory Song.fromJson(dynamic json) {
return Song(
id: json['id'] as int,
title: json['title'] as String,
artist: json['artist'] as String,
lyrics: json['lyrics'] as String,
genre: json['genre'] as String,
rank: json['rank'] as int,
created: json['created'] as String);
}
@override
String toString() {
return 'Song{id: $id, '
'title: $title, '
'artist: $artist, '
'lyrics: $lyrics, '
'genre: $genre, '
'rank: $rank, '
'created: $created}';
}
}
您的song
类似乎是错误的,
试试这个
class Song {
int id;
String title;
List<SongList> songList;
DateTime created;
Song({
this.id,
this.title,
this.songList,
this.created,
});
factory Song.fromJson(Map<String, dynamic> json) => Song(
id: json["id"],
title: json["title"],
songList: List<SongList>.from(json["songList"].map((x) => SongList.fromJson(x))),
created: DateTime.parse(json["created"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"title": title,
"songList": List<dynamic>.from(songList.map((x) => x.toJson())),
"created": created.toIso8601String(),
};
}
class SongList {
int id;
String title;
String artist;
String lyrics;
String genre;
int rank;
DateTime created;
SongList({
this.id,
this.title,
this.artist,
this.lyrics,
this.genre,
this.rank,
this.created,
});
factory SongList.fromJson(Map<String, dynamic> json) => SongList(
id: json["id"],
title: json["title"],
artist: json["artist"],
lyrics: json["lyrics"],
genre: json["genre"],
rank: json["rank"],
created: DateTime.parse(json["created"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"title": title,
"artist": artist,
"lyrics": lyrics,
"genre": genre,
"rank": rank,
"created": created.toIso8601String(),
};
}
我刚刚使用您提供的json创建了一个示例
以下是json:
{
"id": 1,
"title": "The Hits",
"songList": [{
"id": 1,
"title": "hello",
"artist": "me",
"lyrics": "say stuff",
"genre": "rock",
"rank": 10,
"created": "2020-04-06T22:00:00.000"
}, {
"id": 2,
"title": "howdy",
"artist": "you",
"lyrics": "say no",
"genre": "hillBilly",
"rank": 9,
"created": "2019-04-06T22:00:00.000"
}, {
"id": 3,
"title": "Ola",
"artist": "we",
"lyrics": "say nothing",
"genre": "Flapp",
"rank": 2,
"created": "2018-04-06T22:00:00.000"
}],
"created": "2018-04-06T22:00:00.000"
}
稍后是您提供的json的模式:
// To parse this JSON data, do
//
// final song = songFromJson(jsonString);
import 'dart:convert';
Song songFromJson(String str) => Song.fromJson(json.decode(str));
String songToJson(Song data) => json.encode(data.toJson());
class Song {
int id;
String title;
List<SongList> songList;
DateTime created;
Song({
this.id,
this.title,
this.songList,
this.created,
});
factory Song.fromJson(Map<String, dynamic> json) => Song(
id: json["id"],
title: json["title"],
songList: List<SongList>.from(json["songList"].map((x) => SongList.fromJson(x))),
created: DateTime.parse(json["created"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"title": title,
"songList": List<dynamic>.from(songList.map((x) => x.toJson())),
"created": created.toIso8601String(),
};
}
class SongList {
int id;
String title;
String artist;
String lyrics;
String genre;
int rank;
DateTime created;
SongList({
this.id,
this.title,
this.artist,
this.lyrics,
this.genre,
this.rank,
this.created,
});
factory SongList.fromJson(Map<String, dynamic> json) => SongList(
id: json["id"],
title: json["title"],
artist: json["artist"],
lyrics: json["lyrics"],
genre: json["genre"],
rank: json["rank"],
created: DateTime.parse(json["created"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"title": title,
"artist": artist,
"lyrics": lyrics,
"genre": genre,
"rank": rank,
"created": created.toIso8601String(),
};
}
这是从json:中呈现ui的主文件
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dummy.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<SongList> songList = List();
bool _isLoading = false;
@override
void initState() {
super.initState();
loadYourData();
}
Future<String> loadFromAssets() async {
return await rootBundle.loadString('json/parse.json');
}
loadYourData() async {
setState(() {
_isLoading = true;
});
String jsonString = await loadFromAssets();
final song = songFromJson(jsonString);
// here you get the complete object
songList = song.songList;
setState(() {
_isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
child: _isLoading
? CircularProgressIndicator()
: ListView.builder(
itemCount: songList.length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: Column(
children: <Widget>[
Text(songList[index].artist),
Text(songList[index].genre),
Text(songList[index].id.toString()),
Text(songList[index].title)
],
),
);
},
),
),
),
);
}
}
如果有效,请告诉我。