gpt4 book ai didi

flutter - NoSuchMethodError JSON

转载 作者:行者123 更新时间:2023-12-03 04:28:08 29 4
gpt4 key购买 nike

运行以下代码时出现 NoSuchMethodError - 我想从 JSON url 打印出轨道标题 - 我错过了什么吗?

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

}
}

最佳答案

您的问题没有具体的 Dart 或 flutter - 我检查了您的端点返回的数据(在下面粘贴和格式化),它的结构与您的预期不符。

尤其:

  • 数据中没有顶级Track
  • 有一个顶级字段data包含音乐文件条目列表
  • 每个音乐文件条目包含 track field 。这是您尝试解析的字段。

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

    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"
    }
    ]
    }

    关于 flutter - NoSuchMethodError JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57864136/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com