Ashtav
Ashtav

Reputation: 2966

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

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

Upvotes: 4

Views: 2663

Answers (1)

Figen G&#252;ng&#246;r
Figen G&#252;ng&#246;r

Reputation: 12559

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.

Upvotes: 11

Related Questions