0

I have a problem fetching data from a json api. I have this structure:

{
    "team": {
        "team_name": "Team Name",
        "team_quantity": 10,
        "team_victory": 2,
    }
}

and I'm trying to fetch data with Flutter:

class Team{
 final String name;
 final int quantity;
 final int victory;

  Team({this.name, this.quantity, this.victory});

  factory Team.fromJson(Map<String, dynamic> json){
    return Team(
      name: json['team_name'] as String,
      quantity: json['team_quantity'] as int,
      victory: json['team_victory'] as int,
    );
  }
}

Future<List<Team>> fetchTeam(http.Client client) async {
  final response =
      await http.get('url');
  return compute(parseTeam, response.body);
}

List<Team> parseTeam(String responseBody) {
  final parsed = jsonDecode(responseBody).cast<Map<String, dynamic>>();

  return parsed.map<Team>((json) => Team.fromJson(json)).toList();
}

I get this error:

I/flutter (25834): Exception: NoSuchMethodError: Class '_InternalLinkedHashMap<String, dynamic>' has no instance method 'cast' with matching arguments.
I/flutter (25834): Receiver: _LinkedHashMap len:1
I/flutter (25834): Tried calling: cast<Map<String, dynamic>>()
I/flutter (25834): Found: cast<RK, RV>() => Map<RK, RV>

Could you explain the error(s) to me? Thanks so much!

Maybe my problem could be the "team" in the api? I will try fetching data with the same structure in Flutter:

{
    "team": {
        "team_name": "Team Name",
        "team_quantity": 10,
        "team_victory": 2,
    }
}

or

{
   "team_name": "Team Name",
   "team_quantity": 10,
   "team_victory": 2,
}

2 Answers 2

1

try replacing

final parsed = jsonDecode(responseBody).cast<Map<String, dynamic>>();

with

final Map<String, dynamic> parsed = jsonDecode(responseBody);
Sign up to request clarification or add additional context in comments.

9 Comments

i already seen it, but doesn't work for me... the problem could be the "team:{...}" ? I will edit the question with an exmple (thanks for the reply!)
Is it throwing the same error when you make the change ?
yes, i edited the main post if you want help me... I don't understand how can i modify my structure
I tested my solution with your code and json, and it solves the error. Maybe you have the same error somewhere else ? Also, is your Flutter up-to-date ? If not, run flutter upgrade.
i don't know how to return with the new parsed. How can i change the return: return parsed.map<Team>((json) => Team.fromJson(json)).toList();
|
0

Your code seems fine just use this

{
   "team_name": "Team Name",
   "team_quantity": 10,
   "team_victory": 2,
}

also make sure you have these imports

import 'dart:async' show Future;
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

if you want use with Teams then wrap teams with another class like league

 class League {
 List<Team> teams;
League({this.teams});
factory League.fromJson(Map<String, dynamic> json,) {
    if (json["team"] != null) {
      var teams1= json["team"] as List;
      List<Team> _teams=
      teams1.map((list) => Team.fromJson(list)).toList();
      return League(
        teams: _teams,
      );
    }
    return null;
  }
}

and this is how you fetch data

Future<League> getLeague() async {
  final response = await http.Client().get('$url');
  if (response.statusCode == 200) {
    return League.fromJson(json.decode(response.body));
  }
  return null;
}

4 Comments

Thanks for the reply, i can't use this, i can use only the first with "team", how can i modify the code to fetch data in this case
Don't work for me... where you use the "teams1" variable? Did you test it? Thanks again for the help
sorry about that i editted again. this must work imo.
oh ok perfect, i get always the same error... this part is correct? Future<List<League>> fetchLeague(http.Client client) async { final response = await http.get('url'); return compute(parseLeague, response.body); } List<League> parseLeague(String responseBody) { final parsed = jsonDecode(responseBody).cast<Map<String, dynamic>>(); return parsed.map<League>((json) => League.fromJson(json)).toList(); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.