Flutter 如何识别;类型';列表<;字符串>';不是类型为';字符串';“铸入式”;
我理解这个错误是什么,但我无法确定是哪部分代码引起的 我如何识别和理解导致这种情况的代码的哪一部分 我已经创建了一些非常基本的单元测试来验证我的fromJson/jsonDecode和toJson/jsonEncode函数,在使用嵌套类/列表时,除了fromJson之外,其他所有功能都正常工作 当使用toJson/jsonEncode时,我能够成功地生成以下字符串Flutter 如何识别;类型';列表<;字符串>';不是类型为';字符串';“铸入式”;,flutter,dart,Flutter,Dart,我理解这个错误是什么,但我无法确定是哪部分代码引起的 我如何识别和理解导致这种情况的代码的哪一部分 我已经创建了一些非常基本的单元测试来验证我的fromJson/jsonDecode和toJson/jsonEncode函数,在使用嵌套类/列表时,除了fromJson之外,其他所有功能都正常工作 当使用toJson/jsonEncode时,我能够成功地生成以下字符串 {"id":1,"title":"The Hits","songList":[{"id":1,"title":"hello","ar
{"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/Decode会因为cast错误而失败
单元测试
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}';
}
}
看来你的
歌曲
课错了
试试这个
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(),
};
}
class歌曲{
int-id;
字符串标题;
歌曲列表;
创建日期时间;
歌({
这个身份证,
这个名字,
这是我的歌单,
这个,创造,,
});
工厂歌曲.fromJson(映射json)=>歌曲(
id:json[“id”],
标题:json[“标题”],
songList:List.from(json[“songList”].map((x)=>songList.fromJson(x)),
created:DateTime.parse(json[“created”]),
);
映射到JSON()=>{
“id”:id,
“头衔”:头衔,
“songList”:List.from(songList.map((x)=>x.toJson()),
“已创建”:已创建.toiso8601字符串(),
};
}
班级歌曲列表{
int-id;
字符串标题;
弦乐艺术家;
弦乐词;
弦乐体裁;
整数秩;
创建日期时间;
歌曲列表({
这个身份证,
这个名字,
这位艺术家,
这是歌词,
这种类型,
这个军衔,
这个,创造,,
});
factory SongList.fromJson(映射json)=>SongList(
id:json[“id”],
标题:json[“标题”],
艺术家:json[“艺术家”],
歌词:json[“歌词”],
流派:json[“流派”],
等级:json[“等级”],
created:DateTime.parse(json[“created”]),
);
映射到JSON()=>{
“id”:id,
“头衔”:头衔,
“艺术家”:艺术家,
“歌词”:歌词,
“体裁”:体裁,
“等级”:等级,
“已创建”:已创建.toiso8601字符串(),
};
}
我刚刚使用您提供的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"
}
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)
],
),
);
},
),
),
),
);
}
}
稍后是您提供的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数据,请执行以下操作
//
//最终歌曲=songFromJson(jsonString);
导入“dart:convert”;
Song-songFromJson(String-str)=>Song.fromJson(json.decode(str));
字符串songToJson(歌曲数据)=>json.encode(data.toJson());
班歌{
int-id;
字符串标题;
歌曲列表;
创建日期时间;
歌({
这个身份证,
这个名字,
这是我的歌单,
这个,创造,,
});
工厂歌曲.fromJson(映射json)=>歌曲(
id:json[“id”],
标题:json[“标题”],
songList:List.from(json[“songList”].map((x)=>songList.fromJson(x)),
created:DateTime.parse(json[“created”]),
);
映射到JSON()=>{
“id”:id,
“头衔”:头衔,
“songList”:List.from(songList.map((x)=>x.toJson()),
“已创建”:已创建.toiso8601字符串(),
};
}
班级歌曲列表{
int-id;
字符串标题;
弦乐艺术家;
弦乐词;
弦乐体裁;
整数秩;
创建日期时间;
歌曲列表({
这个身份证,
这个名字,
这位艺术家,
这是歌词,
这种类型,
这个军衔,
这个,创造,,
});
factory SongList.fromJson(映射json)=>SongList(
id:json[“id”],
标题:json[“标题”],
艺术家:json[“艺术家”],
歌词:json[“歌词”],
流派:json[“流派”],
等级:json[“等级”],
created:DateTime.parse(json[“created”]),
);
映射到JSON()=>{
“id”:id,
“头衔”:头衔,
“艺术家”:艺术家,
“歌词”:歌词,
“体裁”:体裁,
“等级”:等级,
“已创建”:已创建.toiso8601字符串(),
};
}
这是通过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)
],
),
);
},
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
导入“dummy.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
MyApp({Key}):超级(Key:Key);
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
List songList=List();
bool_isLoading=false;
@凌驾
void initState(){
super.initState();
加载您的数据();
}
Future loadFromAssets()异步{
返回wait-rootBundle.loadString('json/parse.json');
}
loadYourData()异步{
设置状态(){
_isLoading=true;
});
字符串jsonString=await loadFromAssets();
最终歌曲=songFromJson(jsonString);
//这里是完整的对象
songList=song.songList;
设置状态(){
_isLoading=false;
});
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
主体:容器(
子项:_正在加载
?循环压缩机指示器()
:ListView.builder(
itemCount:songList.length,
itemBuilder:(构建上下文,int索引){
回程卡(
子:列(
儿童:[
文本(歌曲列表[index].artist),
文本(歌曲列表[索引].流派),
文本(歌曲列表[index].id.toString()),
文本(歌曲列表[索引].标题)
],
),
);
},
),
),
),
);
}
}
让我知道它是否有效。您能检查堆栈跟踪中哪一行导致错误吗?谢谢您的回复。