-2

So I was able to fetch data for 'locations' in this json using the number (0,1,2,3,4). But I was not able to fetch data from 'prayer_times' string directly. Is there any way to solve this?

I have tried Text(data["date"] because it cannot start with string right away and will give error The argument type 'dart.core::String' can't be assigned to the parameter type 'dart.core::int'.

The api is working do check the link thanks.

Data fetch display code

          Card(
                child: Container(
                    padding: EdgeInsets.all(15.0),
                    child: Row(
                      children: <Widget>[
                        Text("Name: "),
                        Text(data[0]["date"],
                            style: TextStyle(
                                fontSize: 18.0, color: Colors.black87)),
                      ],
                    )),
              ),

API URI code

  final String url = "http://api.azanpro.com/times/today.json?zone=ngs02&format=12-hour";
  List data;

  Future<String> getSWData() async {
    var res = await http
        .get(Uri.encodeFull(url), headers: {"Accept": "application/json"});

    setState(() {
      var resBody = json.decode(res.body);
      data = resBody["prayer_times"];
    });
vicevirus
  • 437
  • 2
  • 4
  • 11
  • you forgot to include JSON input data – Marcin Orlowski Dec 26 '18 at 20:01
  • can you explain please.. sorry I am still quite new to this language. – vicevirus Dec 26 '18 at 20:04
  • paste JSON data you get when you fetch from your `url`. Your problem is not fetching (this part of your question is completely irrelevant). You simply cannot properly access data you loaded. So paste data you are loading. noone is going to guess what you have there – Marcin Orlowski Dec 26 '18 at 20:06

1 Answers1

1

You just need to make two changes.

Change the type of data to a Map and depending on your use case, initialise it to a default value:

Map<String, dynamic> data = {'date': "-------"};

And then get the date field directly in data

        Card(
          child: Container(
              padding: EdgeInsets.all(15.0),
              child: Row(
                children: <Widget>[
                  Text("Name: "),
                  Text(data["date"],
                      style: TextStyle(
                          fontSize: 18.0, color: Colors.black87)),
                ],
              )),
        ),
chemamolins
  • 19,400
  • 5
  • 54
  • 47