skip to Main Content

I am new to Flutter & dart but have a had a blast learning about them, currently I am stuck on trying to fetch data inside the app I have followed the docs Flutter Docs created the model file and the Future to call the api all code is bellow. I have done some research and tried a couple of things but nothing has solved the error type 'int' is not a subtype of 'string' this is most likely a simple error but any pointers is appreciated.

Apologies if any info is missed I had to type this in a hurry. Let me know if anything else is required!

API Used: https://valorant-api.com/v1/currencies

API Response
{
    "status": 200,
    "data": [
        {
            "uuid": "85ad13f7-3d1b-5128-9eb2-7cd8ee0b5741",
            "displayName": "VP",
            "displayNameSingular": "VP",
            "displayIcon": "https://media.valorant-api.com/currencies/85ad13f7-3d1b-5128-9eb2-7cd8ee0b5741/displayicon.png",
            "largeIcon": "https://media.valorant-api.com/currencies/85ad13f7-3d1b-5128-9eb2-7cd8ee0b5741/largeicon.png",
            "assetPath": "ShooterGame/Content/Currencies/Currency_AresPoints_DataAsset"
        },
        {
            "uuid": "85ca954a-41f2-ce94-9b45-8ca3dd39a00d",
            "displayName": "Dough",
            "displayNameSingular": "Dough",
            "displayIcon": "https://media.valorant-api.com/currencies/85ca954a-41f2-ce94-9b45-8ca3dd39a00d/displayicon.png",
            "largeIcon": "https://media.valorant-api.com/currencies/85ca954a-41f2-ce94-9b45-8ca3dd39a00d/largeicon.png",
            "assetPath": "ShooterGame/Content/Currencies/Currency_Dough_DataAsset"
        },
        {
            "uuid": "f08d4ae3-939c-4576-ab26-09ce1f23bb37",
            "displayName": "Free Agents",
            "displayNameSingular": "Free Agent",
            "displayIcon": "https://media.valorant-api.com/currencies/f08d4ae3-939c-4576-ab26-09ce1f23bb37/displayicon.png",
            "largeIcon": "https://media.valorant-api.com/currencies/f08d4ae3-939c-4576-ab26-09ce1f23bb37/largeicon.png",
            "assetPath": "ShooterGame/Content/Currencies/Currency_RecruitmentToken_DataAsset"
        },
        {
            "uuid": "e59aa87c-4cbf-517a-5983-6e81511be9b7",
            "displayName": "Radianite Points",
            "displayNameSingular": "Radianite Point",
            "displayIcon": "https://media.valorant-api.com/currencies/e59aa87c-4cbf-517a-5983-6e81511be9b7/displayicon.png",
            "largeIcon": "https://media.valorant-api.com/currencies/e59aa87c-4cbf-517a-5983-6e81511be9b7/largeicon.png",
            "assetPath": "ShooterGame/Content/Currencies/Currency_UpgradeToken_DataAsset"
        }
    ]
}
Model file
class CurrencyModel {
  final String status;
  final int uuid;
  final String displayName;
  final String displayNameSingular;
  final String displayIcon;
  final String largeIcon;
  final String assetPath;

  const CurrencyModel({
    required this.status,
    required this.uuid,
    required this.assetPath,
    required this.displayIcon,
    required this.displayName,
    required this.displayNameSingular,
    required this.largeIcon,
  });

  factory CurrencyModel.fromJson(Map<String, dynamic> json) {
    return CurrencyModel(
      status: json['status'],
      uuid: int.parse(json['data']['uuid']),
      assetPath: json['data']['assestPath'], 
      displayIcon: json['data']['displayIcon'], 
      displayName: json['data']['displayName'], 
      displayNameSingular: json['data']['displayNameSingular'], 
      largeIcon: json['data']['largeIcon'],
    );
  }
}
Data Fetch file
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:valoranttools/models/models.dart';

Future<CurrencyModel> fetchCurrency() async {
  final response =
      await http.get(Uri.parse('https://valorant-api.com/v1/currencies'));

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response,
    // then parse the JSON.
    print(response.body);
    return CurrencyModel.fromJson(jsonDecode(response.body));
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load album');
  }
}

5

Answers


  1. The problem is in your Currencymodel you are trying to add a string to int

    change this line final int uuid; to this final String uuid;

    as @OMi Shah mentioned you need to remove int.parse and use it like this uuid: json['data']['uuid'],

    if you have a problem with your status, then you need to parse it to int like this int.parse(json['status']),

    Login or Signup to reply.
  2. ex:

    class CurrencyModel {
      final int? status;
      final List<Data>? data;
    
      CurrencyModel({
        this.status,
        this.data,
      });
    
      CurrencyModel.fromJson(Map<String, dynamic> json)
        : status = json['status'] as int?,
          data = (json['data'] as List?)?.map((dynamic e) => Data.fromJson(e as Map<String,dynamic>)).toList();
    
      Map<String, dynamic> toJson() => {
        'status' : status,
        'data' : data?.map((e) => e.toJson()).toList()
      };
    }
    
    class Data {
      final String? uuid;
      final String? displayName;
      final String? displayNameSingular;
      final String? displayIcon;
      final String? largeIcon;
      final String? assetPath;
    
      Data({
        this.uuid,
        this.displayName,
        this.displayNameSingular,
        this.displayIcon,
        this.largeIcon,
        this.assetPath,
      });
    
      Data.fromJson(Map<String, dynamic> json)
        : uuid = json['uuid'] as String?,
          displayName = json['displayName'] as String?,
          displayNameSingular = json['displayNameSingular'] as String?,
          displayIcon = json['displayIcon'] as String?,
          largeIcon = json['largeIcon'] as String?,
          assetPath = json['assetPath'] as String?;
    
      Map<String, dynamic> toJson() => {
        'uuid' : uuid,
        'displayName' : displayName,
        'displayNameSingular' : displayNameSingular,
        'displayIcon' : displayIcon,
        'largeIcon' : largeIcon,
        'assetPath' : assetPath
      };
    }
    
    // your code
    
    Map<String, dynamic> json = {
        "status": 200,
        "data": [
          {
            "uuid": "85ad13f7-3d1b-5128-9eb2-7cd8ee0b5741",
            "displayName": "VP",
            "displayNameSingular": "VP",
            "displayIcon":
                "https://media.valorant-api.com/currencies/85ad13f7-3d1b-5128-9eb2-7cd8ee0b5741/displayicon.png",
            "largeIcon":
                "https://media.valorant-api.com/currencies/85ad13f7-3d1b-5128-9eb2-7cd8ee0b5741/largeicon.png",
            "assetPath":
                "ShooterGame/Content/Currencies/Currency_AresPoints_DataAsset"
          },
          {
            "uuid": "85ca954a-41f2-ce94-9b45-8ca3dd39a00d",
            "displayName": "Dough",
            "displayNameSingular": "Dough",
            "displayIcon":
                "https://media.valorant-api.com/currencies/85ca954a-41f2-ce94-9b45-8ca3dd39a00d/displayicon.png",
            "largeIcon":
                "https://media.valorant-api.com/currencies/85ca954a-41f2-ce94-9b45-8ca3dd39a00d/largeicon.png",
            "assetPath": "ShooterGame/Content/Currencies/Currency_Dough_DataAsset"
          }
        ]
      };
        
     CurrencyModel cModel = CurrencyModel.fromJson(
          json); // responseJson (json) - Map<String, dynamic>
    
      print(cModel.data!.length);
    
    
    `
    
    Login or Signup to reply.
  3. Change
    final int uuid; to final String uuid;
    in the model class


    Change
    uuid: int.parse(json['data']['uuid']), to uuid: json['data']['uuid']

    Login or Signup to reply.
  4. because you are trying to parse the "uuid" field from the API response as an int

    class CurrencyModel {
          final int status;
          final String uuid;
          final String displayName;
          final String displayNameSingular;
          final String displayIcon;
          final String largeIcon;
          final String assetPath;
        
          const CurrencyModel({
            required this.status,
            required this.uuid,
            required this.assetPath,
            required this.displayIcon,
            required this.displayName,
            required this.displayNameSingular,
            required this.largeIcon,
          });
        
          factory CurrencyModel.fromJson(Map<String, dynamic> json) {
            return CurrencyModel(
              status: json['status'],
              uuid: json['data']['uuid'],
              assetPath: json['data']['assetPath'], 
              displayIcon: json['data']['displayIcon'], 
              displayName: json['data']['displayName'], 
              displayNameSingular: json['data']['displayNameSingular'], 
              largeIcon: json['data']['largeIcon'],
            );
          }
        }
    
    Login or Signup to reply.
  5. // To parse this JSON data, do
    //
    //     final currencyModel = currencyModelFromJson(jsonString);
    
    import 'dart:convert';
    
    CurrencyModel currencyModelFromJson(String str) => CurrencyModel.fromJson(json.decode(str));
    
    String currencyModelToJson(CurrencyModel data) => json.encode(data.toJson());
    
    class CurrencyModel {
        int status;
        List<Datum> data;
    
        CurrencyModel({
            required this.status,
            required this.data,
        });
    
        factory CurrencyModel.fromJson(Map<String, dynamic> json) => CurrencyModel(
            status: json["status"],
            data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
        );
    
        Map<String, dynamic> toJson() => {
            "status": status,
            "data": List<dynamic>.from(data.map((x) => x.toJson())),
        };
    }
    
    class Datum {
        String uuid;
        String displayName;
        String displayNameSingular;
        String displayIcon;
        String largeIcon;
        String assetPath;
    
        Datum({
            required this.uuid,
            required this.displayName,
            required this.displayNameSingular,
            required this.displayIcon,
            required this.largeIcon,
            required this.assetPath,
        });
    
        factory Datum.fromJson(Map<String, dynamic> json) => Datum(
            uuid: json["uuid"],
            displayName: json["displayName"],
            displayNameSingular: json["displayNameSingular"],
            displayIcon: json["displayIcon"],
            largeIcon: json["largeIcon"],
            assetPath: json["assetPath"],
        );
    
        Map<String, dynamic> toJson() => {
            "uuid": uuid,
            "displayName": displayName,
            "displayNameSingular": displayNameSingular,
            "displayIcon": displayIcon,
            "largeIcon": largeIcon,
            "assetPath": assetPath,
        };
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search