DEFL
DEFL

Reputation: 1052

Flutter invalid Argument when fetching data

I am getting this error when trying to fetch data from a Api:

The following ArgumentError was thrown building StreamBuilder(dirty, state: _StreamBuilderBaseState<String, AsyncSnapshot>#735fb):

Invalid argument(s)

The function causing this problem is fetchSong()

This is the Code for fetching and displaying(Commenting out all lines in ui that display data in the ui solves the problem):

   class _RadioBarState extends State<RadioBar> {
    Map<String, dynamic> song = {};
    

    fetchSong() async {
      var response = await http.get(
          "https://api.radioking.io/widget/radio/leipzig-beatz/track/current");
      if (response.statusCode == 200) {
        var items = json.decode(response.body);
        setState(
          () {
            song = items;
          },
        );
      }
    }


    @override
    Widget build(BuildContext context) {
      return Row(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Expanded(
            child: GestureDetector(
              onTap: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => RadioDetail(),
                  ),
                );
              },
              child: Container(
                    decoration: BoxDecoration(
                      color: Colors.pinkAccent,
                    ),
                    child: ListTile(
                      tileColor: Palette.bottomBarColor,
                      title: Text(
                        song['title'] ?? "N/A",
                        style:
                            TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
                      ),
                      subtitle: Text(
                        song['album'] ??
                            "N/A" + " \u2219 " + song['artist'] ??
                            "N/A",
                        style: TextStyle(
                            fontSize: 14, color: Palette.radioSubtileColor),
                      ),
                      trailing: Consumer<RadioProvider>(
                        builder: (context, data, child) {
                          return CircleAvatar(
                            backgroundColor: Colors.white,
                            child: IconButton(
                              color: Colors.black,
                              onPressed: () async {
                                await _flutterRadioPlayer.playOrPause();
                              },
                              icon: snapshot.data ==
                                      FlutterRadioPlayer.flutter_radio_playing
                                  ? Icon(Icons.pause)
                                  : Icon(Icons.play_arrow),
                            ),
                          );
                        },
                      ),
                      leading: CircleAvatar(
                        backgroundImage: NetworkImage(
                          song['cover'] ?? "N/A",
                        ),
                      ),
                    ),
                  );
                },
              ),
            ),
          ),
          StreamBuilder<String>(
            initialData: "",
            stream: _flutterRadioPlayer.metaDataStream,
            builder: (context, snapshot) {
              fetchSong();
              return Container();
            },
          ),
        ]
  }

When I comment out all lines in the ui out that takes data from the fetchSong function e.g song['cover'],song['title'] etc. The error disappears.

Upvotes: 1

Views: 277

Answers (1)

Aiman_Irfan
Aiman_Irfan

Reputation: 375

Try putting "Future" before the fetchSong(), and then in the stream, try change the "String" to "RadioBar" class

Upvotes: 1

Related Questions