I'm trying to write json data from a third-party site on a flater, but I've already tried different options. I get the same problem on the last step. Please help me very much.
i have this model
class SkinModel {
String src;
String name;
double minprice;
int count;
double avgprice;
double medianprice;
int popularity;
SkinModel(
{required this.src,
required this.name,
required this.minprice,
required this.count,
required this.avgprice,
required this.medianprice,
required this.popularity});
factory SkinModel.fromJson(Map<String, dynamic> json) {
return SkinModel(
src: json['src'],
name: json['name'],
minprice: json['minprice'],
count: json['count'],
avgprice: json['avgprice'],
medianprice: json['medianprice'],
popularity: json['popularity'],
);
}
}
and such a class for receiving and processing data
import 'dart:convert';
import 'package:flutter_application_1/data_skins.dart';
import 'package:flutter_application_1/model/model_skin.dart';
import 'package:http/http.dart' as http;
class Skins {
Future<dynamic>? getSkinsPopular(String game, String? method) async {
late List<SkinModel> skinListModel = [];
if (method == null) {
skinListModel = skinssamplelist;
}
String url = 'https://market.dota2.net/api/v2/prices/USD.json';
print('Step 1 ' url);
http.Response response = await http.get(
Uri.parse(url),
);
print('Step 2 ' response.reasonPhrase!);
if (response.statusCode == 200) {
var decodeData = json.decode(response.body);
var rest = decodeData['items'] as List;
print('Step 3');
print(rest);
skinListModel =
rest.map<SkinModel>((json) => SkinModel.fromJson(json)).toList();
print('Step 4');
print(skinListModel);
} else {
throw 'Problem with get request';
}
return skinListModel;
}
}
}
But the last successful step 3 and after the debugger writes flutter: Sorry try again I absolutely do not understand what the error is and what I did wrong
CodePudding user response:
late List<SkinModel> skinListModel = [];
String url = 'https://market.dota2.net/api/v2/prices/USD.json';
print('Step 1 ' url);
http.Response response = await http.get(
Uri.parse(url),
);
print('Step 2 ' response.reasonPhrase!);
if (response.statusCode == 200) {
var decodeData = jsonDecode(response.body);
var rest = decodeData['items'] as List;
print('Step 3');
print(rest);
for (var element in rest) {skinListModel.add(SkinModel.fromJson(element)); } //replace this line
print('Step 4');
print(skinListModel);
} else {
throw 'Problem with get request';
}
CodePudding user response:
You are trying to send a request to https://market.dota2.net/api/v2/prices/USD.json and it returns items array of object. Object has fields:
market_hash_name: String,
volume: String,
price: String,
In your code you are trying to parse the json, but your SkinModel has these fields:
String src;
String name;
double minprice;
int count;
double avgprice;
double medianprice;
int popularity;
While converting json to your SkinModel, you try to get values from fields that don't exist. The error occurs in this line.
src: json['src'], // You try to get src but it does not exist
Flutter says that you cannot transform null to String.
_TypeError (type 'Null' is not a subtype of type 'String')
In order to fix error, use another api url. Or change your model to this:
class SkinModel {
final String market_hash_name;
final String volume;
final String price;
}