How can I use widget parameters as variables? - flutter

I cant use the parameter variable.
class HomePage extends StatefulWidget {
const HomePage({
Key? key,
required this.userState,
required this.currentPageForRoute,
}) : super(key: key);
final bool userState;
final int currentPageForRoute;
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int currentPage = widget.currentPageForRoute;
//And then, i going to change currentPage often and use it.
}
I want use widget.currentPageForRoute as currentPage variable, but i cant.
(If I can use it, I will change it often.)
The error massage: Undefined name 'widget'.

You can use initState method or late
late int currentPage = widget.currentPageForRoute;

Related

Accessing route parameters in Dart and flutter

I am sending 2 parameters restaurantDetails.rest_latitude and restaurantDetails.rest_longitude to a class to get the map details
BarMapWidget(restaurantDetails.rest_latitude, restaurantDetails.rest_longitude),
In the class I have a stateful widget
class BarMapWidget extends StatefulWidget {
String restLatitude;
String restLongitude;
BarMapWidget(this.restLatitude, this.restLongitude, {Key? key}) : super(key: key);
#override
_BarMapWidgetState createState() => _BarMapWidgetState();
}
I am passing a string so I know I have to make it a double
Here is the extended class,
class _BarMapWidgetState extends State<BarMapWidget> {
// Destination Longitude
final double _destLatitude = restLatitude;
final double _destLongitude = restLongitude;
void initState() {
// Add destination marker
_addMarker(
LatLng(_destLatitude, _destLongitude),
"destination",
// BitmapDescriptor.defaultMarkerWithHue(90),
BitmapDescriptor.defaultMarker
);
super.initState();
}
Which give me a Undefined name 'restLatitude'. and Undefined name 'restLongitude'.
How can I access the variable in BarMapWidget?
Your restLatitude and restLongitude on widget level, try using widget.variableName. Also, those aren't matching datatype.
If you need double convert BarMapWidget variable to double.
To initialize on state level you can trick with using late or use initState;
class BarMapWidget extends StatefulWidget {
final double restLatitude;
final double restLongitude;
const BarMapWidget(this.restLatitude, this.restLongitude, {Key? key})
: super(key: key);
#override
_BarMapWidgetState createState() => _BarMapWidgetState();
}
class _BarMapWidgetState extends State<BarMapWidget> {
// Destination Longitude
late final double _destLatitude = widget.restLatitude;
late final double _destLongitude = widget.restLongitude;
Just add in
widget.restLatitude

The instance member 'key' can't be accessed in an initializer

So basically I got an error that says
The instance member 'key' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expression when I try to make a StatefulWidget as shown below
class UserPage extends StatefulWidget {
UserData userData;
UserPage(this.userData) : super(key: key);
#override
State<StatefulWidget> createState() => new _UserPageState(userData);
}
any solution for this one?
I tried to add 'late' at every point but it doesn't seem to work.
You should do something like this:
class UserPage extends StatefulWidget {
const UserPage({required this.userData, Key? key}) : super(key: key);
final UserData userData;
#override
State<UserPage> createState() => _UserPageState();
}
The key param is not always required. So you can just delete the super part.
class UserPage extends StatefulWidget {
UserData userData;
UserPage(this.userData);
#override
State<StatefulWidget> createState() => new _UserPageState(userData);
}

Don't put any logic in createState after installing flutter_lints in Flutter

I installed flutter_lints plugin in my project, after installing then it shows a warning message "Don't put any logic in createState". How to solve this issue?
class OverviewPage extends StatefulWidget {
final int id;
const OverviewPage({Key? key, required this.id}) : super(key: key);
#override
_OverviewPageState createState() => _OverviewPageState(id); // Warning on this line
}
class _OverviewPageState extends State<OverviewPage>{
late final int id;
_OverviewPageState(this.id);
}
Don't pass anything to _OverviewPageState in the constructor.
class OverviewPage extends StatefulWidget {
final int id;
const OverviewPage({Key? key, required this.id}) : super(key: key);
#override
_OverviewPageState createState() => _OverviewPageState();
}
class _OverviewPageState extends State<OverviewPage>{
// if you need to reference id, do it by calling widget.id
}
I someone want to initiate a variable inside the state from the main class you can use for example, cause you can't use it in constructor class.
#override
void initState() {
super.initState();
id = widget.id;
code = widget.code;
//your code here
}

How to receive a parameter and use in initState within a stateful widget

I have a stateful widget that has one method called in initialisation. I wanna know how to be able to get a parameter from the previous screen and pass it in initState to my initialisation method
class LabDetalheWidget extends StatefulWidget {
final String path;
const LabDetalheWidget({
Key key,
this.path,
}) : super(key: key);
You can pass parameter like that
class MyWidget extends StatefulWidget {
final String param;
const MyWidget({
Key key,
this.param,
}) : super(key: key);
#override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
#override
void initState() {
print(widget.param);
super.initState();
}
#override
Widget build(BuildContext context) {
return Container(
);
}
}
Inside the state you can access the parameter like that
print(widget.param)
I believe you wanna pass data across routes.. If so then read this flutter cookbook's section you might get the idea
https://flutter.dev/docs/cookbook/navigation/passing-data

How to access the variable from one class to other in flutter

I want to access the variable UniversityId in the class of _HomePageState
class showDetailOfUniversity extends StatelessWidget {
final String UniversityId;
showDetailOfUniversity({Key key, #required this.UniversityId}) : super(key:
key);
#override
Widget build(BuildContext context)
{
return (
HomePage()
);
}
}
class HomePage extends StatefulWidget {
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
//
var temp2 = showDetailOfUniversity();
var temp = temp2.getUniversityId;
/// here i want to access the code but failed
}
The problem here is that you created another instance of showDetailOfUniversity that will have another values for its members. You did not initialize String UniversityId so its value is null untill you set it.
So when you called showDetailOfUniversity() in temp, the value of String UniversityId in this instance is null since there was no value given in this particular instance.
You can pass the String UniversityId in the constructor of the StatefulWidget like this:
class ShowDetailOfUniversity extends StatelessWidget {
final String universityId;
ShowDetailOfUniversity({Key key, #required this.universityId})
: super(key: key);
#override
Widget build(BuildContext context) {
return HomePage(universityId: universityId);
}
}
class HomePage extends StatefulWidget {
final String universityId;
const HomePage({Key key, this.universityId}) : super(key: key);
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
var universityId;
#override
void initState() {
super.initState();
universityId = widget.universityId;
}
#override
Widget build(BuildContext context) {
// TODO: implement build
return Text(universityId);
}
}
Solution for your code:
var temp2 = showDetailOfUniversity(UniversityId: university_id);
var temp = temp2.UniversityId; //this will return university_id