I'm trying to call an API. This is my model :
// To parse this JSON data, do
//
// final hisselist = hisselistFromJson(jsonString);
import 'dart:convert';
Hisselist hisselistFromJson(String str) => Hisselist.fromJson(json.decode(str));
String hisselistToJson(Hisselist data) => json.encode(data.toJson());
class Hisselist {
Hisselist({
required this.success,
required this.result,
});
bool success;
List<ResultClass> result;
factory Hisselist.fromJson(Map<String, dynamic> json) => Hisselist(
success: json["success"], result: json["result"].map<ResultClass>((x) => ResultClass.fromJson(x)).toList(),
);
Map<String, dynamic> toJson() => {
"success": success,
"result": result.map((x) => x.toJson()),
};
}
class ResultClass {
ResultClass({
required this.rate,
required this.lastprice,
required this.lastpricestr,
required this.hacim,
required this.hacimstr,
required this.text,
required this.code,
});
double rate;
double lastprice;
String lastpricestr;
double hacim;
String hacimstr;
String text;
String code;
factory ResultClass.fromJson(Map<String, dynamic> json) => ResultClass(
rate: json["rate"].toDouble(),
lastprice: json["lastprice"].toDouble(),
lastpricestr: json["lastpricestr"],
hacim: json["hacim"].toDouble(),
hacimstr: json["hacimstr"],
text: json["text"],
code: json["code"],
);
Map<String, dynamic> toJson() => {
"rate": rate,
"lastprice": lastprice,
"lastpricestr": lastpricestr,
"hacim": hacim,
"hacimstr": hacimstr,
"text": text,
"code": code,
};
}
This is where i call the API
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import '../models/apis/hisselist.dart';
class Hisseler extends StatefulWidget {
const Hisseler({Key? key}) : super(key: key);
@override
State<Hisseler> createState() => _HisselerState();
}
class _HisselerState extends State<Hisseler> {
final scaffoldKey = GlobalKey<ScaffoldState>();
final url = Uri.parse('https://api.collectapi.com/economy/hisseSenedi');
var counter;
Hisselist? hisseResult;
Future callHisse() async {
try{
Map<String, String> requestHeaders = {
'Content-Type': 'application/json',
'Authorization': 'apikey 3xxx'
};
final response = await http.get(url,headers:requestHeaders);
if(response.statusCode == 200){
var result = hisselistFromJson(response.body);
if(mounted);
setState(() {
counter = result.result.length;
hisseResult = result;
});
return result;
} else {
print(response.statusCode);
}
} catch(e) {
print(e.toString());
}
}
@override
void initState() {
// TODO: implement initState
super.initState();
callHisse();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: false,
automaticallyImplyLeading: false,
title: Text(
'Hisseler'
),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: counter != null ?
ListView.builder(
itemCount: counter,
itemBuilder: (context, index){
return Card(
child: ListTile(
title: Text(hisseResult?.result[index].code??""),
subtitle: Text(hisseResult?.result[index].text??""),
),
);
}) : Center(child: CircularProgressIndicator(
)),
),
),
);
}
}
This is the error.
I'm using same structure from same api website in the same app but other api has no double in it.
How can i fix this? Thanks for your help
**Api response(Its longer i had to cut because of characters limit here) :
{
"success": true,
"result": [
{
"rate": 1.63,
"lastprice": 62.4,
"lastpricestr": "62,40",
"hacim": "",
"hacimstr": "₺6.338.850.855,10",
"min": 61.25,
"minstr": "61,25",
"max": 63.35,
"maxstr": "63,35",
"time": "18:09",
"text": "THYAO - TURK HAVA YOLLARI",
"code": "https:THYAO"
},
{
"rate": -0.7,
"lastprice": 10,
"lastpricestr": "10,00",
"hacim": "",
"hacimstr": "₺2.494.844.604,08",
"min": 9.89,
"minstr": "9,89",
"max": 10.39,
"maxstr": "10,39",
"time": "18:09",
"text": "AKBNK - AKBANK",
"code": "https:AKBNK"
},
{
"rate": -3.2,
"lastprice": 5.44,
"lastpricestr": "5,44",
"hacim": "",
"hacimstr": "₺2.444.674.050,01",
"min": 5.39,
"minstr": "5,39",
"max": 5.76,
"maxstr": "5,76",
"time": "18:09",
"text": "YKBNK - YAPI VE KREDI BANK.",
"code": "https:YKBNK"
},
{
"rate": -0.08,
"lastprice": 24.22,
"lastpricestr": "24,22",
"hacim": "",
"hacimstr": "₺1.993.997.946,36",
"min": 23.88,
"minstr": "23,88",
"max": 24.5,
"maxstr": "24,50",
"time": "18:09",
"text": "SISE - SISE CAM",
"code": "https:SISE"
},
{
"rate": -1.91,
"lastprice": 23.68,
"lastpricestr": "23,68",
"hacim": "",
"hacimstr": "₺1.903.546.516,50",
"min": 23.3,
"minstr": "23,30",
"max": 24.26,
"maxstr": "24,26",
"time": "18:09",
"text": "SAHOL - SABANCI HOLDING",
"code": "https:SAHOL"
},
{
"rate": 1.91,
"lastprice": 24.56,
"lastpricestr": "24,56",
"hacim": "",
"hacimstr": "₺1.878.629.757,06",
"min": 23.84,
"minstr": "23,84",
"max": 24.76,
"maxstr": "24,76",
"time": "18:09",
"text": "ASELS - ASELSAN",
"code": "https:ASELS"
},
{
"rate": 4.47,
"lastprice": 9.81,
"lastpricestr": "9,81",
"hacim": "",
"hacimstr": "₺1.660.601.149,10",
"min": 9.35,
"minstr": "9,35",
"max": 9.9,
"maxstr": "9,90",
"time": "18:09",
"text": "PETKM - PETKIM",
"code": "https:PETKM"
},
{
"rate": -1.77,
"lastprice": 17.75,
"lastpricestr": "17,75",
"hacim": "",
"hacimstr": "₺1.636.411.291,63",
"min": 17.62,
"minstr": "17,62",
"max": 18.37,
"maxstr": "18,37",
"time": "18:09",
"text": "GARAN - GARANTI BANKASI",
"code": "https:GARAN"
},
{
"rate": -0.19,
"lastprice": 52.6,
"lastpricestr": "52,60",
"hacim": "",
"hacimstr": "₺1.514.105.128,80",
"min": 51.8,
"minstr": "51,80",
"max": 53.55,
"maxstr": "53,55",
"time": "18:09",
"text": "SASA - SASA POLYESTER",
"code": "https:SASA"
},
{
"rate": -3.09,
"lastprice": 5.96,
"lastpricestr": "5,96",
"hacim": "",
"hacimstr": "₺1.486.079.838,32",
"min": 5.96,
"minstr": "5,96",
"max": 6.23,
"maxstr": "6,23",
"time": "18:09",
"text": "ISCTR - IS BANKASI (C)",
"code": "https:ISCTR"
},
{
"rate": -3.01,
"lastprice": 3.54,
"lastpricestr": "3,54",
"hacim": "",
"hacimstr": "₺1.374.459.094,21",
"min": 3.49,
"minstr": "3,49",
"max": 3.7,
"maxstr": "3,70",
"time": "18:09",
"text": "EKGYO - EMLAK KONUT GMYO",
"code": "https:EKGYO"
}
]
}
CodePudding user response:
The issue is with double hacim
, it is getting string response like "hacim": "",
.
It would better to use .tryParse
instead of toDouble()
Remove hacim: json["hacim"].toDouble()
and Do it like
hacim: double.tryParse(json["hacim"]) ?? 0.0,
Also better do it for others double as well.
CodePudding user response:
You need to parse the jsonString correctly to double
. The double
class has a double? tryParse(str)
method to parse string
into double
.
double hacimDouble = double.tryParse(json['hacim']??'0.0') ?? 0.0;
And, when you want to use hacimDouble
, then do hacimDouble.toString()
.