【问题标题】:I am a beginner to flutter so, help me with this thing我是一个初学者,所以请帮我解决这个问题
【发布时间】:2023-01-22 20:56:02
【问题描述】:

我收到类似“String is not a subtype of type int of index

`[
    {
      "id": "1",
      "name": "Deva",
      "age": "20",
      "address": {
        "streetname": "Park street",
        "area": "Jjnagar",
        "place": "Pollachi",
        "pincode": "642001"
    },
      "phonenumber": "8877665544"
    }
] `

这是我的 json 数据
我需要在我的代码中解析

`body: FutureBuilder(
            future:
                DefaultAssetBundle.of(context).loadString("assets/data.json"),
            builder: (context, snapshot) {
              var mydata = json.decode(snapshot.data.toString());
              return Center(
                child: Text(
                  mydata['name'],
                  style: const TextStyle(
                    color: Colors.deepPurple,
                    fontWeight: FontWeight.bold,
                    fontSize: 25,
                  ),
                ),
              );
            }));`

这是代码
如何解决

【问题讨论】:

    标签: json flutter dart


    【解决方案1】:

    实际上它是一个列表所以你可以像这样访问它

    body: FutureBuilder(
            future:
                DefaultAssetBundle.of(context).loadString("assets/data.json"),
            builder: (context, snapshot) {
              
              var mydata = json.decode(snapshot.data.toString());
              final mapData=mydata[0];
              return Center(
                child: Text(
                  mapData['name'],
                  style: const TextStyle(
                    color: Colors.deepPurple,
                    fontWeight: FontWeight.bold,
                    fontSize: 25,
                  ),
                ),
              );
            }));`
    

    【讨论】:

      【解决方案2】:

      当您尝试将字符串值用作需要整数值的列表或数组的索引时,通常会出现此错误消息。

      例如,假设您有一个名为 myList 的整数列表,并且您正尝试使用字符串变量访问列表中的一个元素:

      List<int> myList = [1, 2, 3];
      String index = "1";
      print(myList[index]); // This will cause the error "String is not a subtype of type int of index"

      要修复此错误,您需要先将字符串变量转换为整数,然后再将其用作索引。您可以使用 int.parse() 方法将字符串转换为整数:

      List<int> myList = [1, 2, 3];
      String index = "1";
      print(myList[int.parse(index)]); // This will print the value 2

      或者,您可以使用 .toInt() 方法将字符串转换为整数

      List<int> myList = [1, 2, 3];
      String index = "1";
      print(myList[index.toInt()]); // This will print the value 2

      当您将字符串值传递给任何需要整数值的函数或方法时,您也会遇到同样的错误。在这种情况下,您需要将字符串转换为 int,反之亦然。

      【讨论】:

        【解决方案3】:

        您看到的错误消息“String is not a subtype of type int of index”表明您尝试解析的 json 数据是一个字符串,但您的代码正在尝试访问它,就好像它是一个 int 一样。

        要解决此问题,您应该更改行我的数据['名称']我的数据[0][‘名字’],因为您的 json 数据似乎是一个对象列表,其中每个对象都包含“姓名”、“年龄”等字段。

        您正在尝试直接从 json 对象访问 name 属性,但它在对象列表中,因此您需要通过索引列表的第一个对象然后索引其中的属性(如 mydata[0]['name'])来访问它。

        此外,请确保您尝试解析的 json 数据格式正确并且与您的 json 数据的结构相匹配。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-09-30
          • 1970-01-01
          • 1970-01-01
          • 2017-04-15
          • 2023-02-22
          • 2022-11-26
          相关资源
          最近更新 更多