dart 如何在列表“EngName”(Flutter)中仅获取子列表值“曼谷”(类型:列表< dynamic>)

s3fp2yjn  于 12个月前  发布在  Flutter
关注(0)|答案(1)|浏览(120)

我想在“EngName”列表中获得“曼谷”的值
x1c 0d1x->这是我的JSON数据
这是我的dart代码:

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_report/RainfallThai.dart';
import 'package:http/http.dart' as http;
import 'package:syncfusion_flutter_charts/charts.dart';

class LineCharts extends StatefulWidget {
  const LineCharts({super.key});

  @override
  State<LineCharts> createState() => _DashBoardDBState();
}

class _DashBoardDBState extends State<LineCharts> {
  List<RainfallThai> usersA = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Line Charts"),
        backgroundColor: Colors.pink[300],
      ),
      body: Container(
        child: SfCartesianChart(
            primaryXAxis: CategoryAxis(),
            title: ChartTitle(text: 'ปริมาณน้ำฝน'),
            legend: Legend(
              isVisible: true,
            ),
            tooltipBehavior: TooltipBehavior(enable: true),
            series: <ChartSeries<RainfallThai, String>>[
              LineSeries<RainfallThai, String>(
                dataSource: usersA,
                xValueMapper: (RainfallThai monthai, _) => monthai.MonThai,
                yValueMapper: (RainfallThai avgrain, _) =>
                    num.tryParse(avgrain.AvgRain),
                dataLabelSettings: DataLabelSettings(isVisible: true),
              ),
            ]),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: fetchUsersA,
        backgroundColor: Colors.amber,
        child: Icon(Icons.auto_graph_rounded),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    fetchUsersA();
  }

  void fetchUsersA() async {
    print('LINE CHART called');
    const urlA = "https://v1.nocodeapi.com/synatic/csv2json/pOWwVHFulerJvWnC";
    final uriA = Uri.parse(urlA);
    final responseA = await http.get(uriA);
    final bodyA = responseA.body;
    final jsonsA = jsonDecode(bodyA);
    final jsonA = jsonsA['json'] as List<dynamic>;
    final transformedA = jsonA.map((obj) {
      print(obj['EngName']);
      return RainfallThai(
          ProvinceName: '',
          EngName: obj['EngName'],
          toID: '',
          MonThai: obj['MonThai'],
          MinRain: '',
          MaxRain: '',
          AvgRain: obj['AvgRain'],
          region: '',
          Year: obj['Year'],
          Month: '',
          Date: '');
    }).toList();

    setState(() {
      usersA = transformedA;
    });
    print('LINE CHART completed');
  }
}

字符串
这就是->的输出

print(obj['EngName']);


输出


->打印输出图像
总之,我想知道如何输出,只显示值的子列表,
扑:曼谷
并非子列表中的所有数据

e3bfsja2

e3bfsja21#

更改此行

final jsonA = jsonsA['json'] as List<dynamic>;

字符串

final jsonA = (jsonsA['json'] as List<dynamic>).sublist(0,1);

相关问题