skip to Main Content

I am making a app that is playing a radio station but I got the error and i don’t know how to fix it. I have looked to the other answers from stackoverflow but i don’t get how i need to implement it. Can anyone help me to fix this error?

This is my console:

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): 

This is the code to receive the data from AzuraCast and set a string with the data received:

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

The string returns always empty because of it.

the whole responds body:

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

2

Answers


  1. The response is an array (the starting "[").
    So first you need to find which station you want, the first will be data[0].

    If you remove some of the fields you are not using from the response you can see:

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

    This shows that each object in the returned array has "station", and "now_playing" properties – so to get the song from the first now playing object you want data[0]["now_playing"]["song"]

    Login or Signup to reply.
  2. as you can look there is list at the top of the response so if you want to extract data you have to first go into the list using index like this:

    final song = Song.fromJson(data[0]['station']['now_playing']['song']);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search