Flutter http api 获取 json 密钥

Flutter http api get json key

我在一个变量中有json这样的数据,我怎样才能一个一个地使用这里的数据。

我想这样使用

开始:07.00 - 结束:22.00

 Padding(
              padding: const EdgeInsets.only(left: 6, top:2,right: 5,bottom:5),
              child: Align(
                alignment: Alignment.centerLeft,
                child: Container(
                  child: Text(widget.company.worktime.toString(),style: TextStyle(fontSize: 16),),

                ),
              ),

            ),

结果;

[{"start":"07:00","end":"22:00","close":0},
{"start":"07:00","end":"22:00","close":0},
{"start":"07:00","end":"22:00","close":0},
{"start":"07:00","end":"22:00","close":0},
{"start":"07:00","end":"22:00","close":0},
{"start":"07:00","end":"22:00","close":0},
{"start":"07:00","end":"22:00","close":0}]

需要最少的代码来确定。 但是如果 json 数据被容纳在一个数组中,你可以调用 index data

或者可以用这个 https://api.flutter.dev/flutter/widgets/ListView-class.html

创建Worktimemodel/dataclass

class Worktime {
  String? start;
  String? end;
  int? close;

  Worktime({this.start, this.end, this.close});

  Worktime.fromJson(Map<String, dynamic> json) {
    start = json['start'];
    end = json['end'];
    close = json['close'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = {};
    data['start'] = start;
    data['end'] = end;
    data['close'] = close;
    return data;
  }
}

创建一个json列表数据解析器方法

List<Worktime> wrktimesFromJson(String str) =>
    List<Worktime>.from(jsonDecode(str).map((x) => Worktime.fromJson(x)));

如果您的列表数据很大并且您需要更好的性能,那么请尝试使用 ListView.builder 小部件,否则为了简单使用 case/small 数据集请使用 Column with SingleChildScrollView.

对于ListView.builder:

class _MyHomePageState extends State<MyHomePage> {
  List<Worktime> worktimes = [];

  @override
  void initState() {
    worktimes = wrktimesFromJson(widget.company.worktime.toString());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Padding(
        padding: const EdgeInsets.only(left: 6, top: 2, right: 5, bottom: 5),
        child: ListView.builder(
          itemCount: worktimes.length,
          itemBuilder: (BuildContext context, int index) {
            var worktime = worktimes[index];

            return Text(
              "Start : ${worktime.start} - End : ${worktime.end}",
              style: const TextStyle(fontSize: 16),
            );
          },
        ),
      ),
    );
  }
}

对于带有 SingleChildScrollView 的列:

class _MyHomePageState extends State<MyHomePage> {
  List<Worktime> worktimes = [];

  @override
  void initState() {
    worktimes = wrktimesFromJson(widget.company.worktime.toString());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.only(left: 6, top: 2, right: 5, bottom: 5),
          child: Column(
            children: worktimes.map((worktime) {
              return Text(
                "Start : ${worktime.start} - End : ${worktime.end}",
                style: const TextStyle(fontSize: 16),
              );
            }).toList(),
          ),
        ),
      ),
    );
  }
}