title: How to create a dynamic tab bar in Flutter?
body: <p>I have two tabs that are built from the repository class. I need to filter the items that are shown on them according to nationalities, but the items are rendered only on the first tab. The second tab doesn't show anything, I believe it has to do with Stateful and Stateless.</p>
  
  <p>How can I run repository class each time I change tab? </p>
  
  <p>The code below is about Tab and TabView</p>
  
  <pre><code>import 'package:flutter/material.dart';
  import 'package:flutterbol/models/teams_model.dart';
  import 'package:flutterbol/repository/teams_repository.dart';
  
  class TeamsScreen extends StatefulWidget {
    @override
    _TeamsScreenState createState() =&gt; _TeamsScreenState();
  }
  
  class _TeamsScreenState extends State&lt;TeamsScreen&gt; {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: DefaultTabController(
          length: choices.length,
          child: Scaffold(
            appBar: AppBar(
              title: const Text('Tabbed AppBar'),
              bottom: TabBar(
                isScrollable: true,
                tabs: choices.map((Choice choice) {
                  return Tab(
                    text: choice.title,
                    icon: Icon(choice.icon),
                  );
                }).toList(),
              ),
            ),
            body: TabBarView(
              children: choices.map((Choice choice) {
                return Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: ChoiceCard(choice: choice),
                );
              }).toList(),
            ),
          ),
        ),
      );
    }
  }
  
  class Choice {
    const Choice({this.title, this.icon});
  
    final String title;
    final IconData icon;
  }
  
  const List&lt;Choice&gt; choices = const &lt;Choice&gt;[
    const Choice(title: 'NATIONALS', icon: Icons.flag),
    const Choice(title: 'INTERNATIONALS', icon: Icons.outlined_flag),
  ];
  
  class ChoiceCard extends StatelessWidget {
    const ChoiceCard({Key key, this.choice}) : super(key: key);
  
    final Choice choice;
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
          body: FutureBuilder&lt;List&lt;TeamsModel&gt;&gt;(
            future: TeamsRepository().findAllAsync(),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                return buildListView(snapshot.data);
              } else {
                return Center(
                  child: CircularProgressIndicator(),
                );
              }
            },
          ));
    }
  
    ListView buildListView(List&lt;TeamsModel&gt; teams) {
      return ListView.builder(
        itemCount: teams == null ? 0 : teams.length,
        //itemCount: teams.length,
        itemBuilder: (BuildContext ctx, int index) {
          return teamCard(teams[index]);
        },
      );
    }
  
    Card teamCard(TeamsModel team) {
      if (team.nacionalidade == choice.title) {
        return Card(
          child: Text(team.name),
        );
      }
    }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/n5A4w.jpg" rel="nofollow noreferrer">Tabs and Cards</a></p>