Can you try the following. I generated the class using https://app.quicktype.io/
Change your StopInfo class as
// To parse this JSON data, do
//
// final stopInfo = stopInfoFromJson(jsonString);
import 'dart:convert';
class StopInfo {
StopInfo({
this.bus,
this.stops,
});
List<String> bus;
List<String> stops;
factory StopInfo.fromJson(Map<String, dynamic> json) => StopInfo(
bus: List<String>.from(json["Bus"].map((x) => x)),
stops: List<String>.from(json["Stops"].map((x) => x)),
);
Map<String, dynamic> toJson() => {
"Bus": List<dynamic>.from(bus.map((x) => x)),
"Stops": List<dynamic>.from(stops.map((x) => x)),
};
}
and to convert back and forth use these functions
List<StopInfo> stopInfoFromJson(String str) => List<StopInfo>.from(json.decode(str).map((x) => StopInfo.fromJson(x)));
String stopInfoToJson(List<StopInfo> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
You probably need to use the first one and use the list to create the display widgets. The following is a minimal example
class _MyHomePageState extends State<MyHomePage> {
String busStops = '''
[{
"Bus": ["a", "b", "c"],
"Stops": ["1", "2", "3", "4"]
}]
''';
List<StopInfo> allStops = [];
List<Widget> cardsList = [];
@override
void initState() {
super.initState();
loadPrelimData();
}
void loadPrelimData(){
setState(() {
allStops.clear();
allStops = stopInfoFromJson(busStops);
fillColumnChildren();
});
}
void fillColumnChildren(){
cardsList.clear();
allStops.forEach((stopInfos) {
String stops = "";
stopInfos.stops.forEach((element) { stops += element + ", "; });
stopInfos.bus.forEach((element) {
cardsList.add(
//the widget that will be displayed
Card(
child: ListTile(title: Text("Bus:" + element),
subtitle: Text("Stops:" + stops),
),
)
);
});
});
}
List<StopInfo> stopInfoFromJson(String str) => List<StopInfo>.from(json.decode(str).map((x) => StopInfo.fromJson(x)));
String stopInfoToJson(List<StopInfo> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Bus stops"),),
body: Center(
child: Column(children: cardsList)
));
}
}
I get the following
