Does removing code duplication from CupertinoTabView impact performance due to absence of const modifier

20 Views Asked by At

I'm very new to flutter and dart, and I'm building a simple IOS/Cupertino mobile app. When creating a CupertinoTabBar, I followed some tutorials and created the tabBuilder like so:

      tabBuilder: (context, index) {
        return switch (index) {
          0 => CupertinoTabView(
              builder: (context) => const CupertinoPageScaffold(
                child: AudioPlayerTab(),
              ),
            ),
          1 => CupertinoTabView(
              builder: (context) => const CupertinoPageScaffold(
                child: LibraryTab(),
              ),
            ),
          2 => CupertinoTabView(
              builder: (context) => const CupertinoPageScaffold(
                child: AudioPlayerTab(),
              ),
            ),
          3 => CupertinoTabView(
              builder: (context) => const CupertinoPageScaffold(
                child: AudioPlayerTab(),
              ),
            ),
          _ => throw Exception('Invalid index $index'),
        };

As you can see, there is a lot of code duplication here, so I thought I would change that.

So I tried this, and it works, but I'm concerned about performance:

        buildTab(widget) => CupertinoTabView(
              builder: (_) => CupertinoPageScaffold(child: widget),
            );
        return switch (index) {
          0 => buildTab(const AudioPlayerTab()),
          1 => buildTab(const LibraryTab()),
          2 => buildTab(const LibraryTab()),
          3 => buildTab(const LibraryTab()),
          _ => throw Exception('Invalid index $index'),
        };

I find this code a lot cleaner. However, my only concern is that it might not be as performant, because I no longer have a const before the CupertinoPageScaffold (though my IDE recommended I put one in front of e.g. AudioPlayerTab, but I don't exactly know why). Is this less performant due to the absence of const? What does that const do anyways? Is it good practice to remove code duplication like this? Thanks!

0

There are 0 best solutions below