Home > Net >  "Invalid constant value" error for TabController
"Invalid constant value" error for TabController

Time:04-14

I've worked with tabs before and this code bellow always worked. But I recently updated everything and as usual they broke something that was working perfectly and now I don't know how to make it work.

It's the exact same example as in https://api.flutter.dev/flutter/material/TabController-class.html

Here's my code:

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {  
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 5, vsync: this);
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        bottom: const TabBar(
          controller: _tabController, //problem is here
          tabs: [...

When I assign "controller: _tabController" it throws "Invalid constant value".

I get it, TabBar.controller is expecting a constant. But how to work around this error?

CodePudding user response:

Remove const keyword in front of TabBar widget

  • Related