flutter - 颤振 - NoSuchMethodError JSON
问题描述
运行以下代码时出现 NoSuchMethodError - 我想从 JSON url 打印出曲目标题 - 我错过了什么吗?
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<Track> fetchPost() async {
final response =
await http.get('http://139.59.108.222:2199/rpc/drn1/streaminfo.get');
if (response.statusCode == 200) {
// If the call to the server was successful, parse the JSON.
return Track.fromJson(json.decode(response.body));
} else {
// If that call was not successful, throw an error.
throw Exception('Failed to load post');
}
}
class Track {
String artist;
String title;
String album;
int royaltytrackid;
dynamic started;
int id;
int length;
Playlist playlist;
String buyurl;
String imageurl;
Track({
this.artist,
this.title,
this.album,
this.royaltytrackid,
this.started,
this.id,
this.length,
this.playlist,
this.buyurl,
this.imageurl,
});
factory Track.fromJson(Map<String, dynamic> json) => Track(
artist: json["artist"],
title: json["title"],
album: json["album"],
royaltytrackid: json["royaltytrackid"],
started: json["started"],
id: json["id"],
length: json["length"],
playlist: Playlist.fromJson(json["playlist"]),
buyurl: json["buyurl"],
imageurl: json["imageurl"],
);
Map<String, dynamic> toJson() => {
"artist": artist,
"title": title,
"album": album,
"royaltytrackid": royaltytrackid,
"started": started,
"id": id,
"length": length,
"playlist": playlist.toJson(),
"buyurl": buyurl,
"imageurl": imageurl,
};
}
class Playlist {
int id;
String title;
Playlist({
this.id,
this.title,
});
factory Playlist.fromJson(Map<String, dynamic> json) => Playlist(
id: json["id"],
title: json["title"],
);
Map<String, dynamic> toJson() => {
"id": id,
"title": title,
};
}
void main() => runApp(MyPosts(track: fetchPost()));
class MyPosts extends StatelessWidget {
final Future<Track> track;
MyPosts({Key key, this.track}) : super(key: key);
@override
Widget build(BuildContext context) {
double c_width = MediaQuery.of(context).size.width;
return Container(
padding: const EdgeInsets.all(16.0),
width: c_width,
child: FutureBuilder<Track>(
future: track,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Column(
children: <Widget>[
new Text(snapshot.data.title),
//new Text(snapshot.data.body)
]
);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
//By default, show a loading spinner.
return CircularProgressIndicator();
},
),
);
}
}
解决方案
如果您不确定要编写解析代码,可以试试这个网站。下面是根据json生成的。
class Response {
String type;
List<Data> data;
Response({this.type, this.data});
Response.fromJson(Map<String, dynamic> json) {
type = json['type'];
if (json['data'] != null) {
data = new List<Data>();
json['data'].forEach((v) {
data.add(new Data.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['type'] = this.type;
if (this.data != null) {
data['data'] = this.data.map((v) => v.toJson()).toList();
}
return data;
}
}
class Data {
String title;
String song;
Track track;
String bitrate;
String server;
String autodj;
String source;
bool offline;
String summary;
int listeners;
int maxlisteners;
int reseller;
bool serverstate;
bool sourcestate;
bool sourceconn;
String date;
String time;
String rawmeta;
String mountpoint;
String tuneinurl;
String directtuneinurl;
String proxytuneinurl;
String tuneinformat;
String webplayer;
String servertype;
int listenertotal;
String url;
Data(
{this.title,
this.song,
this.track,
this.bitrate,
this.server,
this.autodj,
this.source,
this.offline,
this.summary,
this.listeners,
this.maxlisteners,
this.reseller,
this.serverstate,
this.sourcestate,
this.sourceconn,
this.date,
this.time,
this.rawmeta,
this.mountpoint,
this.tuneinurl,
this.directtuneinurl,
this.proxytuneinurl,
this.tuneinformat,
this.webplayer,
this.servertype,
this.listenertotal,
this.url});
Data.fromJson(Map<String, dynamic> json) {
title = json['title'];
song = json['song'];
track = json['track'] != null ? new Track.fromJson(json['track']) : null;
bitrate = json['bitrate'];
server = json['server'];
autodj = json['autodj'];
source = json['source'];
offline = json['offline'];
summary = json['summary'];
listeners = json['listeners'];
maxlisteners = json['maxlisteners'];
reseller = json['reseller'];
serverstate = json['serverstate'];
sourcestate = json['sourcestate'];
sourceconn = json['sourceconn'];
date = json['date'];
time = json['time'];
rawmeta = json['rawmeta'];
mountpoint = json['mountpoint'];
tuneinurl = json['tuneinurl'];
directtuneinurl = json['directtuneinurl'];
proxytuneinurl = json['proxytuneinurl'];
tuneinformat = json['tuneinformat'];
webplayer = json['webplayer'];
servertype = json['servertype'];
listenertotal = json['listenertotal'];
url = json['url'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['title'] = this.title;
data['song'] = this.song;
if (this.track != null) {
data['track'] = this.track.toJson();
}
data['bitrate'] = this.bitrate;
data['server'] = this.server;
data['autodj'] = this.autodj;
data['source'] = this.source;
data['offline'] = this.offline;
data['summary'] = this.summary;
data['listeners'] = this.listeners;
data['maxlisteners'] = this.maxlisteners;
data['reseller'] = this.reseller;
data['serverstate'] = this.serverstate;
data['sourcestate'] = this.sourcestate;
data['sourceconn'] = this.sourceconn;
data['date'] = this.date;
data['time'] = this.time;
data['rawmeta'] = this.rawmeta;
data['mountpoint'] = this.mountpoint;
data['tuneinurl'] = this.tuneinurl;
data['directtuneinurl'] = this.directtuneinurl;
data['proxytuneinurl'] = this.proxytuneinurl;
data['tuneinformat'] = this.tuneinformat;
data['webplayer'] = this.webplayer;
data['servertype'] = this.servertype;
data['listenertotal'] = this.listenertotal;
data['url'] = this.url;
return data;
}
}
class Track {
String artist;
String title;
String album;
int royaltytrackid;
Null started;
int id;
int length;
Playlist playlist;
String buyurl;
String imageurl;
Track(
{this.artist,
this.title,
this.album,
this.royaltytrackid,
this.started,
this.id,
this.length,
this.playlist,
this.buyurl,
this.imageurl});
Track.fromJson(Map<String, dynamic> json) {
artist = json['artist'];
title = json['title'];
album = json['album'];
royaltytrackid = json['royaltytrackid'];
started = json['started'];
id = json['id'];
length = json['length'];
playlist = json['playlist'] != null
? new Playlist.fromJson(json['playlist'])
: null;
buyurl = json['buyurl'];
imageurl = json['imageurl'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['artist'] = this.artist;
data['title'] = this.title;
data['album'] = this.album;
data['royaltytrackid'] = this.royaltytrackid;
data['started'] = this.started;
data['id'] = this.id;
data['length'] = this.length;
if (this.playlist != null) {
data['playlist'] = this.playlist.toJson();
}
data['buyurl'] = this.buyurl;
data['imageurl'] = this.imageurl;
return data;
}
}
class Playlist {
int id;
String title;
Playlist({this.id, this.title});
Playlist.fromJson(Map<String, dynamic> json) {
id = json['id'];
title = json['title'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['title'] = this.title;
return data;
}
}
大多数情况下,我自己编写了解析代码,但如果有什么奇怪的地方,我会试一试。希望这有帮助。
编辑:因为 op 没有直接提供 json,链接可能有一天会失效,让我在下面附上它以供进一步阅读:
{
"type": "result",
"data": [
{
"title": "DRN1",
"song": "Juice WRLD - Rider",
"track": {
"artist": "Juice WRLD",
"title": "Rider",
"album": "Death Race for Love",
"royaltytrackid": 21,
"started": null,
"id": 21,
"length": 0,
"playlist": {
"id": 2,
"title": "Standard Rotation"
},
"buyurl": "",
"imageurl": "http://OP.S.IP.ADDRESS:PORT/static/drn1/covers/rsz_emb_juice_wrld_feeling_7bc5490f.jpg" // mosaic this part :P
},
"bitrate": "128 Kbps",
"server": "Online",
"autodj": "Online",
"source": "Yes",
"offline": false,
"summary": "<a href=\"http://OP.S.IP.ADDRESS:PORT/tunein/-stream/drn1.pls\">DRN1 - Juice WRLD - Rider</a>",
"listeners": 1,
"maxlisteners": 100,
"reseller": 0,
"serverstate": true,
"sourcestate": true,
"sourceconn": true,
"date": "Sep 10, 2019",
"time": "01:11 PM",
"rawmeta": "Juice WRLD - Rider ",
"mountpoint": "/stream",
"tuneinurl": "http://OP.S.IP.ADDRESS:PORT/stream", // mosaic this part :P
"directtuneinurl": "",
"proxytuneinurl": "",
"tuneinformat": "mp3",
"webplayer": "muses",
"servertype": "IceCast",
"listenertotal": 1,
"url": "http://OP.S.IP.ADDRESS:PORT/rpc" // mosaic this part :P
}
]
}
推荐阅读
- perl - 使用 perl / PPR 从 perl 文件中删除所有注释
- android - RenderScript 在带有致命信号 6 (SIGABRT) 的 Android 5 (API 21) 上崩溃
- java - java.lang.NoClassDefFoundError:无法初始化类 org.codehaus.groovy.vmplugin.v7.Java7
- php - 如何在动态子域链接 test.domain.com/anything 中重写 url
- c# - 如何从 C# 中字符串变量中的名称调用静态方法?
- mysql - 使用 MySQL 按他们出演的电影类型列出演员
- clickonce - clickonce 自定义包的引导程序未正确计算安装条件
- python - 在 Django 模板中渲染破折号
- declaration - 在另一个过程中调用一个过程,该过程在它之前声明
- android - 为什么我们不能在滚动视图中直接添加多个视图?