Haritha Madhushanka
Haritha Madhushanka

Reputation: 257

How to fix the error type 'String' is not a subtype of type 'int' of 'index'?

I am new to flutter and after following StackOverflow guidances I managed to find how to access specific parts of the values in the json tree and print those values of specific keys in a map. But now I'm getting the error type 'String' is not a subtype of type 'int' of 'index'

This is my code.

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      localizationsDelegates: <LocalizationsDelegate<dynamic>>[
        DefaultMaterialLocalizations.delegate,
        DefaultWidgetsLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate,
      ],
      home: TransactionHistoryScreen(),
    );
  }
}

class TransactionHistoryScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      child: Scaffold(body: Container(child: MakeList(),),),
    );
  }
}

class MakeList extends StatelessWidget {
final List<Map<String, String>> json = [
  {"branch": "B1", "xyz": "0", "ABC": "2", "MN": "2"},
  {"branch": "B2", "xyz": "0", "ABC": "0", "MN": "0"},
];

@override
Widget build(BuildContext context) {
  return ListView.builder(
    itemCount: json.length,
    itemBuilder: (BuildContext context, int index) {
      return ListTileClass(jsonObject: json[index]);
    },
   );
  }
}

class ListTileClass extends StatefulWidget {
final Map<String, String> jsonObject;

ListTileClass({this.jsonObject});

@override
_ListTileClassState createState() => _ListTileClassState();
}

class _ListTileClassState extends State<ListTileClass> {
  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Container(
        child: Row(
          children: makeWidgetChildren(widget.jsonObject),
        ),
      ),
    );
  }

  List<Widget> makeWidgetChildren(jsonObject) {
    List<Widget> children = [];
    jsonObject.keys.forEach(
      (key) => {
        children.add(
          Padding(
              child: Text('${key['branch']}'), padding: EdgeInsets.all(8.0)),
        ),
      },
    );
    return children;
  }
}

Could I please know how to fix this error? Thank you!

Upvotes: 0

Views: 7286

Answers (2)

w461
w461

Reputation: 2698

your error is here:

itemBuilder: (BuildContext context, int index) {
      return ListTileClass(jsonObject: json[index]);

json is a map not a list. So you would call an element by `json["branch"]

Not sure, what the easiest way is to solving this, but creating a list with the keys could help: keyList = json.keys (maybe you have to tweak this a bit to make it work, not tested). Then you can use it with return ListTileClass(jsonObject: json[keyList[index]]);

Upvotes: 1

Adelina
Adelina

Reputation: 11941

You are iterating over jsonObject.keys:

var keys = ["branch", "xyz", "ABC", "MN"] 
keys.forEach((key) => print('${key['branch']}')); # A value of type 'String' can't be assigned to a variable of type 'int'.

So you are trying to iterate over Strings.

To solve t:

Text('${jsonObject[key]}')

Upvotes: 3

Related Questions