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!
question from:
https://stackoverflow.com/questions/65626367/how-to-fix-the-error-type-string-is-not-a-subtype-of-type-int-of-index 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…