Dart : How to fetch data with no number id (string)?

Issue

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"];
    });

Solution

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)),
                ],
              )),
        ),

Answered By – chemamolins

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.