Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get variable from statefulwidget class to state class in dart flutter?

Tags:

flutter

dart

This is my flutter code

import 'package:flutter/material.dart';

class Navbar extends StatefulWidget {
  int tabIndex;
  Navbar(this.tabIndex);

  _NavbarState createState() => _NavbarState();
}

class _NavbarState extends State<Navbar> with SingleTickerProviderStateMixin {
// I need to get tabIndex here
}

I need to get tabIndex in second class, thank you so much for your answers

like image 939
Ashtav Avatar asked Oct 21 '25 06:10

Ashtav


1 Answers

You can create a tabIndex variable in your State class and initialize it in your initState with widget.tabIndex.

class _NavbarState extends State<Navbar> with SingleTickerProviderStateMixin {
// I need to get tabIndex here
int tabIndex;

 @override
 void initState() {
  tabIndex= widget.tabIndex;
  super.initState();
 }
}

Or you can just call it inside build method with widget.tabIndex.

like image 174
Figen Güngör Avatar answered Oct 22 '25 20:10

Figen Güngör