Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index' 更好的解释

问题描述 投票:0回答:2

我正在制作一个正在播放广播电台的应用程序,但出现错误,我不知道如何解决。我已经查看了 stackoverflow 的其他答案,但我不知道我需要如何实现它。谁能帮我解决这个错误?

这是我的控制台:

V/MediaPlayer(18403): resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer(18403): cleanDrmObj: mDrmObj=null mDrmSessionId=null
V/MediaHTTPService(18403): MediaHTTPService(android.media.MediaHTTPService@7866d26): Cookies: null
V/MediaHTTPService(18403): makeHTTPConnection: CookieHandler (java.net.CookieManager@cb6c9fd) exists.
V/MediaHTTPService(18403): makeHTTPConnection(android.media.MediaHTTPService@7866d26): cookieHandler: java.net.CookieManager@cb6c9fd Cookies: null
I/flutter (18403): [{station: {id: 1, name: LeagueFM, shortcode: leaguefm, description: We make the music play!, frontend: icecast, backend: liquidsoap, listen_url: https://radiopaneel.something-something.nl/listen/something/stream, url: https://something-something.nl/, public_player_url: https://radiopaneel.something-something.nl/public/something, playlist_pls_url: https://radiopaneel.something-something.nl/public/something/playlist.pls, playlist_m3u_url: https://radiopaneel.something-something.nl/public/something/playlist.m3u, is_public: true, mounts: [{id: 1, name: 320kbps MP3, url: https://radiopaneel.something-something.nl/listen/something/stream, bitrate: 320, format: mp3, listeners: {total: 14, unique: 5, current: 14}, path: /stream, is_default: true}], remotes: [], hls_enabled: false, hls_url: null, hls_listeners: 0}, listeners: {total: 14, unique: 5, current: 14}, live: {is_live: true, streamer_name: name, broadcast_start: 1678204893, art: null}, now_playing: {sh_id: id, played_at: 1678207342, duration: 0, playlist: , streamer: name, is_request: false, song: {id: 3866ae5415bf7
E/flutter (18403): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
E/flutter (18403): #0      _MyHomePageState.getTrack (package:something/main.dart:96:38)
E/flutter (18403): <asynchronous suspension>
E/flutter (18403): 

这是从 AzuraCast 接收数据并使用接收到的数据设置字符串的代码:

Future<void> getTrack(Set<dynamic> set) async {
    final response = await http.get(
      Uri.parse('https://radiopaneel.something-something.nl/api/nowplaying'),
      headers: {
        'X-API-Key':
        'something',
      },
    );

    if (response.statusCode == 200) {
      final data = jsonDecode(response.body);
      print(data);
      final song = Song.fromJson(data['station']['now_playing']['song']);
print(data);

      final id = song.id;
      final songTitle = song.title;
      final songArtist = song.artist;
      final streamer = song.streamer_name;
      print(id);
print(streamer);
      setState(() {
        titleString = songTitle;
        streamerr = streamer;
      });
    } else {
      throw Exception('Failed to retrieve current track.');
    }
  }


class Song {
  final String id;
  final String title;
  final String artist;
  final String streamer_name;

  Song({required this.id, required this.title, required this.artist, required this.streamer_name});

  factory Song.fromJson(Map<String, dynamic> json) {
    return Song(
      id: json['id'].toString(),
      title: json['title'].toString(),
      artist: json['artist'].toString(),
        streamer_name: json['streamer_name'].toString()
    );
  }
}

字符串因此总是返回空。

全身反应:

[
  {
    "station": {
      "id": 1,
      "name": "something",
      "shortcode": "something",
      "description": "We make the music play!",
      "frontend": "icecast",
      "backend": "liquidsoap",
      "listen_url": 
      "is_public": true,
      "mounts": [
        {
          "id": 1,
          "name": "320kbps MP3",
          "url": "https://radiopaneel.something-something.nl/listen/something/stream",
          "bitrate": 320,
          "format": "mp3",
          "listeners": {
            "total": 9,
            "unique": 4,
            "current": 9
          },
          "path": "/stream",
          "is_default": true
        }
      ],
      "remotes": [],
      "hls_enabled": false,
      "hls_url": null,
      "hls_listeners": 0
    },
    "listeners": {
      "total": 9,
      "unique": 4,
      "current": 9
    },
    "live": {
      "is_live": true,
      "streamer_name": "something",
      "broadcast_start": 1678204893,
      "art": null
    },
    "now_playing": {
      "sh_id": 87956,
      "played_at": 1678206744,
      "duration": 0,
      "playlist": "",
      "streamer": "something",
      "is_request": false,
      "song": {
        "id": "id",
        "text": "Guus Meeuwis - Het Is Een Nacht",
        "artist": "Guus Meeuwis",
        "title": "Het Is Een Nacht",
        "album": "",
        "genre": "",
        "isrc": "",
        "lyrics": "",
        "art": "art",
        "custom_fields": []
      },
      "elapsed": 51,
      "remaining": 0
    },
    "playing_next": {
      "cued_at": 1678204813,
      "played_at": 1678206782,
      "duration": 203,
      "playlist": "Non-Stop",
      "is_request": false,
      "song": {
        "id": "id",
        "text": "Demi Lovato - Sorry Not Sorry",
        "artist": "Demi Lovato",
        "title": "Sorry Not Sorry",
        "album": "Tell Me You Love Me (Deluxe)",
        "genre": "Pop",
        "isrc": "",
        "lyrics": "",
        "art": "id",
        "custom_fields": []
      }
    },
    "song_history": [
      {
        "sh_id": id,
        "played_at": 1678206515,
        "duration": 229,
        "playlist": "",
        "streamer": "something",
        "is_request": false,
        "song": {
          "id": "id",
          "text": "Glass Animals - Heat Waves",
          "artist": "Glass Animals",
          "title": "Heat Waves",
          "album": "",
          "genre": "",
          "isrc": "",
          "lyrics": "",
          "art": "art",
          "custom_fields": []
        }
      },
      {
        "sh_id": id,
        "played_at": 1678206356,
        "duration": 159,
        "playlist": "",
        "streamer": "something",
        "is_request": false,
        "song": {
          "id": "id",
          "text": "text",
          "artist": "artist",
          "title": "title",
          "album": "",
          "genre": "",
          "isrc": "",
          "lyrics": "",
          "art": "art",
          "custom_fields": []
        }
      },
      {
        "sh_id": id,
        "played_at": 1678206092,
        "duration": 264,
        "playlist": "",
        "streamer": "Jules",
        "is_request": false,
        "song": {
          "id": "id",
          "text": "something",
          "artist": "something",
          "title": "Good Time",
          "album": "",
          "genre": "",
          "isrc": "",
          "lyrics": "",
          "art": "art",
          "custom_fields": []
        }
      },
      {
        "sh_id": id,
        "played_at": 1678205913,
        "duration": 179,
        "playlist": "",
        "streamer": "something",
        "is_request": false,
        "song": {
          "id": "id",
          "text": "text",
          "artist": "artist",
          "title": "title",
          "album": "",
          "genre": "",
          "isrc": "",
          "lyrics": "",
          "art": "something",
          "custom_fields": []
        }
      },
      {
        "sh_id": id,
        "played_at": 1678205742,
        "duration": 171,
        "playlist": "",
        "streamer": "something",
        "is_request": false,
        "song": {
          "id": "dc383b8d72296caa9cb5d3851ee5910a",
          "text": "S10 - De Diepte",
          "artist": "S10",
          "title": "De Diepte",
          "album": "",
          "genre": "",
          "isrc": "",
          "lyrics": "",
          "art": "something",
          "custom_fields": []
        }
      }
    ],
    "is_online": true,
    "cache": null
  }
]
android ios flutter dart radio-button
2个回答
1
投票

你可以看到响应顶部有一个列表,所以如果你想提取数据,你必须首先使用这样的索引进入列表:

final song = Song.fromJson(data[0]['station']['now_playing']['song']);

1
投票

响应是一个数组(开始的“[”)。 所以首先你需要找到你想要的电台,第一个是

data[0]
.

如果您从响应中删除一些您不使用的字段,您可以看到:

[
    {
      "station": {
        "id": 1,
        "name": "something",
        ...
      },
      "listeners": {
        "total": 9,
        ...
      },
      "live": {
        "is_live": true,
        ...
      },
      "now_playing": {
        "sh_id": 87956,
        "played_at": 1678206744,
        "duration": 0,
        ...
        "song": {
          "id": "id",
          "text": "Guus Meeuwis - Het Is Een Nacht",
          "artist": "Guus Meeuwis",
          "title": "Het Is Een Nacht",
          ...
        },
      },
      "is_online": true,
      "cache": null
    }
]

这表明返回数组中的每个对象都具有“station”和“now_playing”属性 - 因此要从您想要的第一个正在播放的对象中获取歌曲

data[0]["now_playing"]["song"]

© www.soinside.com 2019 - 2024. All rights reserved.