I have a StatefulWidget class 'FirstClass' which extends a State '_FirstClassState'. From a separate State class, called '_SecondClassState', I'm trying to access the value of a variable (called 'counter' in '_FirstClassState').
I've found a solution, but I'm not sure if this is the best solution to the problem. I've tried looking at other similar questions:
- How to access Stateful widget variable inside State class outside the build method? (Declaring the variable in the StatefulWidget class and using 'widget.' in the State class does not allow me to get the value of 'counter' from '_SecondClassState')
- I've also seen other sites that recommend using a GlobalKey. However, even though this might be a solution, I didn't explore further into this as I found other sites which recommend minimising the use of GlobalKeys
second_class.dart:
import 'package:brew_crew/question/first_class.dart';
import 'package:flutter/material.dart';
class SecondClass extends StatefulWidget {
@override
_SecondClassState createState() => _SecondClassState();
}
class _SecondClassState extends State<SecondClass> {
final FirstClass firstClass = FirstClass();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("MyApp"),
centerTitle: true,
),
body: Column(
children: <Widget>[
firstClass, //Displays the button to increase 'counter'
FlatButton(
child: Text("Submit"),
onPressed: () {
print(firstClass
.getCounter()); //I need to get the value of 'counter' here to use elsewhere
}),
],
),
);
}
}
first_class.dart
import 'package:flutter/material.dart';
class FirstClass extends StatefulWidget {
final _FirstClassState firstClassState = _FirstClassState();
@override
_FirstClassState createState() => firstClassState;
int getCounter() {
return firstClassState.counter;
}
}
class _FirstClassState extends State<FirstClass> {
int counter = 0;
//Increases counter by 1
void _increaseCounter() {
setState(() {
counter++;
});
}
@override
Widget build(BuildContext context) {
//A button which increases counter when clicked
return FlatButton.icon(
icon: Icon(Icons.add),
label: Text("Counter: $counter"),
onPressed: () {
_increaseCounter();
},
);
}
}
As you can see, within 'FirstClass', I initialise a new instance of '_FirstClassState'. Using this in the 'getCounter()' function, I can get the 'counter' value.
Is there a better way (e.g. best practice, fewer lines of code, an easier to understand method, etc.) than this to achieve what I'm trying to do?