扑-NoSuchMethodError JSON

时间:2019-09-10 04:39:22

标签: flutter dart

运行以下代码时出现NoSuchMethodError-我想从JSON网址中打印出曲目标题-我错过了什么吗?

NoSuchMethodError

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();
            },
          ),
        );

  }
}

3 个答案:

答案 0 :(得分:1)

如果不确定要编写解析代码,则可以尝试this site。下面是根据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
    }
  ]
}

答案 1 :(得分:0)

您的问题没有特别的问题-我检查了您的端点返回的数据(粘贴和格式化如下),其结构与您的推测不符。

尤其是:

  • 数据中没有顶级跟踪
  • 有一个顶级字段data,其中包含音乐文件条目列表
  • 每个音乐文件条目均包含track字段。这是您要解析的字段。

获取所有轨道数据的示例代码

var responseJson = json.decode(response.body);
var tracks = responseJson['data']
    .map((musicFileJson) => Track.fromJson(musicFileJson['track']))
    .toList()
    .cast<Track>();

源文件(供参考)

{
  "type": "result",
  "data": [
    {
      "title": "DRN1",
      "song": "JAHBOY - Karma of the Butterfly Effect",
      "track": {
        "artist": "JAHBOY",
        "title": "Karma of the Butterfly Effect",
        "album": "The Green Project",
        "royaltytrackid": 94.0000,
        "started": null,
        "id": 94,
        "length": 0,
        "playlist": {
          "id": 2,
          "title": "Standard Rotation"
        },
        "buyurl": "",
        "imageurl": "http:\/\/139.59.108.222:2197\/static\/drn1\/covers\/rsz_emb_jahboy_dreamz_f1aca1a5.jpg"
      },
      "bitrate": "128 Kbps",
      "server": "Online",
      "autodj": "Online",
      "source": "Yes",
      "offline": false,
      "summary": "<a href=\"http:\/\/139.59.108.222:2199\/tunein\/-stream\/drn1.pls\">DRN1 - JAHBOY - Karma of the Butterfly Effect<\/a>",
      "listeners": 1,
      "maxlisteners": 100,
      "reseller": 0,
      "serverstate": true,
      "sourcestate": true,
      "sourceconn": true,
      "date": "Sep 10, 2019",
      "time": "12:53 PM",
      "rawmeta": "JAHBOY - Karma of the Butterfly Effect ",
      "mountpoint": "\/stream",
      "tuneinurl": "http:\/\/139.59.108.222:8003\/stream",
      "directtuneinurl": "",
      "proxytuneinurl": "",
      "tuneinformat": "mp3",
      "webplayer": "muses",
      "servertype": "IceCast",
      "listenertotal": 1,
      "url": "http:\/\/139.59.108.222:2199\/rpc"
    }
  ]
}

答案 2 :(得分:0)

    Future<Track> fetchPost() async {
      final response =
      await http.get((Uri.parse('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 type;
  List<Data> data;

  Track({this.type, this.data});

  Track.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;
  }
}

您应该使用此网站将JSON转换为CLASS DART(https://javiercbk.github.io/json_to_dart/?fbclid=IwAR3i5zNWd3mwXJwHcUwAd47pxe25XOhPdhan6s_3CH1Cgb9Qic8az3UmZc4