I’m trying to map list of json to tab bar, and want tab bar title as "JSON1"
API response :"data": [{"JSON1": {"key1": "val1","key2": "val2"}}],
My code :
List dataResponse = [{JSON1: {key1: val1, key2: val2}}]; TabBar( tabAlignment: TabAlignment.start, indicatorSize: TabBarIndicatorSize.tab, dividerColor: Theme.of(context).dividerColor, controller: _tabController, indicatorColor: Theme.of(context).colorScheme.primary, labelColor: Theme.of(context).colorScheme.primary, unselectedLabelColor: Theme.of(context).primaryColorDark.withOpacity(0.8), isScrollable: Responsive.isDesktop(context) ? true : true, tabs: dataResponse.map((e) => Text(e)).toList(), );
`
Getting error: Expected a value of type ‘String’, but got one of type JsonMap
2
Answers
You received the error: Expected a value of type ‘String’, but got one of type JsonMap because you are sending the whole json in the Text widget of dataResponse.map((e) => Text(e)).toList()
instead, send only
To resolve the above issue, replace your code from
with the following in tabs:
This code iterates through the list and finds the first key from each json map of the list. here first key is Json1 so you’ll receive output as expected.