How would one go about decoding a JSON String that looks like this and turn it into a Expansion Tile List with the dropdown of each tile being the "ownerhistory" of each tile:
[
{
"bookName": "Harry Potter",
"Ownerhistory": [
{
"name": "Jane",
"address": "USA",
},
{
"name": "Dan",
"address": "Russia",
}
]
},
{
"bookName": "Harry Potter",
"Ownerhistory": [
{
"name": "Mary",
"address": "USA",
},
{
"name": "Ann",
"address": "Mexico",
},
{
"name": "Molly",
"address": "Canada",
}
]
}
]
I pretty much have an idea how to display it, but i could not test it as of the moment since im having a hard time converting the json into a list that i can use to create the widgets
class BookScreen extends ConsumerWidget {
const BookScreen({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
//JSON Arrives here, i already tested this code and confirms that
//i am recieving the JSON String
final jsonStringData = ref.watch(responseProvider); //JSON arrives here
var tagsJson = jsonDecode(jsonStringData);
//Attempt at converting but throws an error and i have no clue what its doing
List<dynamic> tags = tagsJson != null ? List.from(tagsJson) : null;
return Column(
children: <Widget>[
for ( var i in bookList ){ //not currently sure if this is done properly to iterate through the list
ExpansionTile(
title: const Text(i.bookName),
chrildren: <Widgets>[
for ( var j in i.OwnerHistory)
ListTile(title: Text(j));;
]
);
}
],
);
}
}
2
Answers
I don’t prefer doing serialization & deserialization here , you create another dart file and defining the mechanism for JSON handling , in your case will be:
Now you have a list of objects and you can directly use it in your app, guess it would be:
then use the var yourListVar directly when ever and however you want
Try the code below (you can copy & paste to dartpad.dev). But please note that the JSON string in your question is not valid. You need to remove the commas from the lines which are the last in a block, for example
"address": "USA",
should be"address": "USA"
. (I did this to test my answer).