根据 api 响应创建饼图
我想使用API响应创建一个饼图,但它不起作用 以下是我获取数据的方式:
Future getPie() async{
final response = await http.get(Uri.parse("url"));
final js = jsonDecode(response.body);
return js;
}
然后,我在将来的构建器中创建PIE
FutureBuilder(
future: getPie(),
builder: (ctx,AsyncSnapshot ss){
if(ss.hasData){
return SingleChildScrollView(
child: Container(
child: Center(
child: PieChart(
dataMap: ss.data,
chartRadius: MediaQuery.of(context).size.width / 1.7,
legendOptions: LegendOptions(
legendPosition: LegendPosition.bottom
),
)
),
)
);
}else{
return Center(child: CircularProgressIndicator());
}
}
),
这是API响应
{
'a' : 2
'b' : 5
}
,但我会遇到一个错误:
类型'_internallinkedHashmap< string,dynamic>'不是类型'Map< string,double>'
的子类型
我还尝试使用地图,但手动工作,而从API中则无法使用:
Future getPie() async{
final response = await http.get(Uri.parse("url"));
final js = jsonDecode(response.body);
final a= js['a'];
final b= js['b'];
//this work
final Map<String, double> data = {};
data['a'] = 4;
data['b'] = 5;
//this doesn't
final Map<String, double> data = {};
data['a'] = a;
data['b'] = b;
print(data);
return data;
}
I want to create a pie chart using the API response, but it doesn't work
here's how I get the data:
Future getPie() async{
final response = await http.get(Uri.parse("url"));
final js = jsonDecode(response.body);
return js;
}
then I create the pie in a future builder
FutureBuilder(
future: getPie(),
builder: (ctx,AsyncSnapshot ss){
if(ss.hasData){
return SingleChildScrollView(
child: Container(
child: Center(
child: PieChart(
dataMap: ss.data,
chartRadius: MediaQuery.of(context).size.width / 1.7,
legendOptions: LegendOptions(
legendPosition: LegendPosition.bottom
),
)
),
)
);
}else{
return Center(child: CircularProgressIndicator());
}
}
),
this is the API response
{
'a' : 2
'b' : 5
}
but I get an error :
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, double>'
I also tried using a map but manually work and from API doesn't :
Future getPie() async{
final response = await http.get(Uri.parse("url"));
final js = jsonDecode(response.body);
final a= js['a'];
final b= js['b'];
//this work
final Map<String, double> data = {};
data['a'] = 4;
data['b'] = 5;
//this doesn't
final Map<String, double> data = {};
data['a'] = a;
data['b'] = b;
print(data);
return data;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
改为这样做:
Do this instead: