0

I have json data like this in one variable, How can I use the data here one by one.

I want use like this

Start : 07.00 - End : 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),),

                ),
              ),

            ),

Result;

[{"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}]

1
  • First you have to store json response in list and then use that list in ListView as data source. Commented Mar 30, 2022 at 8:41

2 Answers 2

1

Create Worktime model/data class

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;
  }
}

Create a json list data parser method

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

If your list data will be large and you need better performance then try to use ListView.builder widget otherwise for simple use case/small data set use Column with SingleChildScrollView.

For 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),
            );
          },
        ),
      ),
    );
  }
}

For Column with 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(),
          ),
        ),
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

What should I do if I want to create an static array and print the days at the beginning? return Text( "DaysList[idx] : ${worktime.start} - ${worktime.end}", style: const TextStyle(fontSize: 16), )
use string interpolation: return Text( "${DaysList[idx]} : ${worktime.start} - ${worktime.end}", style: const TextStyle(fontSize: 16), )
but [idx] giving error children: worktimes?.map((worktime) { return Text("${DaysList[idx]} : ${worktime.start ?? "-"} ${worktime.end ?? "-"}", style: const TextStyle(fontSize: 16), ); }
0

need minimal code to be sure. but if the json data is accommodated in an array you can call index data

or can use this https://api.flutter.dev/flutter/widgets/ListView-class.html

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.