Dart:如何获取没有数字 id(字符串)的数据?

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

所以我能够使用数字 (0,1,2,3,4) 在 json 中获取 'locations' 的数据。但是我无法直接从 'prayer_times' 字符串中获取数据。有什么办法可以解决吗?

我试过 Text(data["date"] 因为它不能立即以字符串开头,会报错 The argument type 'dart.core::String' can't be assigned to the parameter type 'dart.core::int'.

api 正在工作请检查 link 谢谢。

数据获取显示代码

          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 代码

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

您只需要进行两项更改。

data 的类型更改为 Map 并根据您的用例将其初始化为默认值:

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

然后直接在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)),
                ],
              )),
        ),