简体   繁体   English

TypeError(类型'String'不是'index'类型'int'的子类型)

[英]TypeError (type 'String' is not a subtype of type 'int' of 'index')

This my Code.这是我的代码。 When i run code i'm getting a error like this当我运行代码时,我收到这样的错误
"TypeError (type 'String' is not a subtype of type 'int' of 'index')". “TypeError(类型'String'不是'index'类型'int'的子类型)”。 Is it something Do with Json Data or the I'm using Wrong Data Type.是与 Json 数据有关还是我使用了错误的数据类型。 Does Future<List<Arrivals>> is written correctly? Future<List<Arrivals>>是否正确编写?

    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }

    class _MyHomePageState extends State<MyHomePage> {
      List<Arrivals> details = [];
      Future<List<Arrivals>> _getDetails() async {
        var data =
            await http.get("https://flight-api-maldives.herokuapp.com/arrivals");
        var jsonData = jsonDecode(data.body);

        for (var val in jsonData) {
          Arrivals arrivals = Arrivals(
            val['Scheduled'],
            val['Revised'],
            val['From'],
            val['Flight'],
            val['Status'],
          );
          details.add(arrivals);
          print(details.length);
        }
        return details;
      }

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                FutureBuilder(
                  future: _getDetails(),
                  builder: (BuildContext context, AsyncSnapshot snapshot) {
                    if (snapshot.data != null) {
                      return Container(
                        child: ListView.builder(
                          itemBuilder: (BuildContext context, int index) {
                            return Center(child: Text('snapshot.data[index].Scheduled'),);
                          }
                        ),
                      );
                    }else{
                       return Center(
                        child: Text('NO'),
                      );
                    }
                  },
                )
              ],
            ),
          ),
        );
      }
    } 
    class Arrivals {
      final String Scheduled;
      final String Revised;
      final String From;
      final String Flight;
      final String Status;

      Arrivals(this.Scheduled, this.Revised, this.From, this.Flight, this.Status);
    }

this is the json data im using:这是我使用的 json 数据:

[
  [
    {
      "Scheduled": "06:35",
      "Revised": "06:35",
      "From": "Kaadedhdhoo (KDM)",
      "Flight": "Maldivian Q2 149",
      "Status": "On-Time"
    },
    {
      "Scheduled": "06:40",
      "Revised": "06:40",
      "From": "Dharavandhoo Island (DRV)",
      "Flight": "Maldivian Q2 289",
      "Status": "On-Time"
    },
 ]
]

Where is the picture of my error https://i.stack.imgur.com/qVWLc.png我的错误图片在哪里https://i.stack.imgur.com/qVWLc.png

the main problem is in json.主要问题出在 json 中。 it is list of list.它是列表列表。

 var jsonData = jsonDecode(data.body);
    jsonData = jsonData[0]; // added line
    for (var val in jsonData) {

Moreover their is no need of so many extra code to just display list view you can use following simple code to display list view.此外,它们不需要太多额外的代码来显示列表视图,您可以使用以下简单代码来显示列表视图。 In addition to that Listview.builder require item count property to specify total number of item.除了那个 Listview.builder 需要项目计数属性来指定项目的总数。

Replace below build method with your will work for you.用您的意愿替换以下构建方法。

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder(
        future: _getDetails(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          print(snapshot);
          if (snapshot.data != null) {
            return ListView.builder(
                itemCount: snapshot.data.length,
                itemBuilder: (BuildContext context, int index) {
                  return Center(
                    child: Text(snapshot.data[index].Scheduled.toString()),
                  );
                });
          } else {
            return Center(
              child: Text('NO'),
            );
          }
        },
      ),
    );
  }

In the JSON data, you've got a list inside a list.在 JSON 数据中,列表中有一个列表。

val['Scheduled'],

This line is getting called on a list, so the square brackets are expecting an index rather than a map key此行在列表中被调用,因此方括号需要索引而不是 map 键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 _TypeError(类型'String'不是'index'类型'int'的子类型) - _TypeError (type 'String' is not a subtype of type 'int' of 'index') _Typeerror(类型“String”不是“index”的“int”的子类型)错误 - _Typeerror ( type 'String' is not a subtype of 'int' of 'index' ) Error Flutter/Dart _TypeError(类型'String'不是'index'的'int'类型的子类型) - Flutter/Dart _TypeError (type 'String' is not a subtype of type 'int' of 'index') “String”不是“index”的“int”类型的子类型 - 'String' is not a subtype of type 'int' of 'index' TypeError(类型“int”不是“String”类型的子类型) - TypeError (type 'int' is not a subtype of type 'String') flutter 类型“字符串”不是“索引”的“int”类型的子类型 - flutter type 'String' is not a subtype of type 'int' of 'index' 错误:“String”类型不是“index”类型“int”的子类型 - Error: type 'String' is not a subtype of type 'int' of 'index' 异常“类型&#39;字符串&#39;不是&#39;索引&#39;的&#39;int&#39;类型的子类型” - Exception "type 'String' is not a subtype of type 'int' of 'index'" “String”类型不是“index”的“int”类型的子类型 - Type 'String' is not a subtype of type 'int' of 'index' &#39;String&#39; 类型不是 &#39;index&#39; Flutter 的 &#39;int&#39; 类型的子类型 - type 'String' is not a subtype of type 'int' of 'index' Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM