How do I add tabs dynamically to the TabBar Widget in Flutter? The list of tabs is fetched from the internet using a REST API.
When I fetch the list using the code below I get the following errors:
RangeError (index): Invalid value: Only valid value is 0: 1
A RenderFlex overflowed by 99896 pixels on the bottom.
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Future<List<Categories>> categoriesList;
final categoryTabs = <Tab>[
Tab(text: 'Home'),
];
@override
void initState() {
super.initState();
categoriesList = NewsServices.fetchCategoriesList(lang: 'en');
categoriesList.then((categories) {
categories.forEach((category) {
setState(() {
categoryTabs.add(Tab(
text: category.name,
));
});
});
});
}
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: categoryTabs.length,
child: Scaffold(
appBar: AppBar(
title: Text('Example App'),
bottom: TabBar(
isScrollable: true,
tabs: categoryTabs,
),
),
body: SafeArea(...),
),
),
);
}
