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
The problem is in your
Currencymodel
you are trying to add a string to intchange this line
final int uuid;
to thisfinal String uuid;
as @OMi Shah mentioned you need to remove
int.parse
and use it like thisuuid: 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']),
ex:
Change
final int uuid;
tofinal String uuid;
in the model class
Change
uuid: int.parse(json['data']['uuid'])
, touuid: json['data']['uuid']
because you are trying to parse the "uuid" field from the API response as an int