Home > Blockchain >  I am a beginner to flutter so, help me with this thing
I am a beginner to flutter so, help me with this thing

Time:01-23

I am getting an error like "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"
    }
] `

This is my json data
I need to parse in in my code

`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,
                  ),
                ),
              );
            }));`

This is the code
how to solve it

CodePudding user response:

actually it is a list so you can access it like this

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,
              ),
            ),
          );
        }));`
  • Related