根据 api 响应创建饼图

发布于 2025-01-19 18:49:45 字数 1657 浏览 1 评论 0原文

我想使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魄砕の薆 2025-01-26 18:49:45

改为这样做:

Future getPie() async {
  final response = await http.get(Uri.parse("url"));
  final js = jsonDecode(response.body);
  var res = Map.fromEntries(js.entries.map((e) => MapEntry<String, double>(e.key, e.value.toDouble())));
  return res;
}

Do this instead:

Future getPie() async {
  final response = await http.get(Uri.parse("url"));
  final js = jsonDecode(response.body);
  var res = Map.fromEntries(js.entries.map((e) => MapEntry<String, double>(e.key, e.value.toDouble())));
  return res;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文