How to access the variable from one class to other in flutter - 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

Related

owner._debugCurrentBuildTarget == this is not true

There is no problem with the codes but when I run
I get error owner._debugCurrentBuildTarget == this is not true how can i solve this problem
class HomePage extends StatelessWidget{
#override
HomePageState createState() => HomePageState();
#override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
class HomePageState extends State{
late int first, second;
late String islem;
late String result, sonuc;
I think you are missing the StatefulWidget format, try
class HomePage extends StatefulWidget {
const HomePage({super.key});
#override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late int first, second;
late String islem;
late String result, sonuc;
#override
void initState() {
super.initState();
first = 1;
second = 2;
islem = "islem";
result = "A";
sonuc = "sonuc";
}
#override
Widget build(BuildContext context) {
return Container();
}
}

How to make a screen that show a specific page if there's data to display and show a specific page if there's no data to display

I made 3 dart file that called Cart, CartSection, and EmptyCartState.
here is the code from Cart
class CartPage extends StatefulWidget {
const CartPage({Key? key}) : super(key: key);
#override
_CartPageState createState() => _CartPageState();
}
class _CartPageState extends State<CartPage> {
#override
Widget build(BuildContext context) {
return Scaffold();
}
}
it's still empty.
what I want to know is how to make this Cart show CartSection if there are something in the cart and show EmptyCartState if there is nothin in the cart?
While you like to get empty screen, you can use nullable data. I am using int as datatype, you can use your model in this case.
class CartPage extends StatefulWidget {
const CartPage({
Key? key,
this.data,
}) : super(key: key);
final int? data;
#override
_CartPageState createState() => _CartPageState();
}
class _CartPageState extends State<CartPage> {
#override
Widget build(BuildContext context) {
return widget.data == null
? Text("empty data widget")
: Text("cart data widget");
}
}
Visit dart.dev to learn more about it.

Flutter State<WidgetName> createState() => WidgetNameState()

In Flutter, when initializing a new stateful widget, it is being initialized like this by default:
class WidgetName extends StatefulWidget {
const WidgetName({ Key? key }) : super(key: key);
#override
WidgetNameState createState() => WidgetNameState();
}
I have seen another way of initializing the statefulwidget with #override being slightly different.
class WidgetName extends StatefulWidget {
const WidgetName({ Key? key }) : super(key: key);
#override
State<WidgetName> createState() => WidgetNameState();
}
Notice in the #override method, WidgetNameState became State<WidgetName>. There is an explanation in the Flutter repo that explains it: Link, but I couldn't comprehend what it is trying to say.
What does State<WidgetName> do exactly? Does it give any advantages?
I thought it wouldn't be necessary as WidgetNameState already extends from State<WidgetName> in its class construction.
class WidgetNameState extends State<WidgetName> {
#override
Widget build(BuildContext context) {
Using the generic really let's you define an abstract Widget interface that must be used without having to define any state along with that abstract widget interface.
Let's look at using the concrete class first (WidgetNameState). This is an abstract definition, we must define the state if we do this.
abstract class FooWidget extends StatefulWidget {
const FooWidget({Key? key}) : super(key: key);
#override
_FooWidgetState createState();
}
abstract class _FooWidgetState extends State<FooWidget> {
#override
Widget build(BuildContext context) {
return Container();
}
}
Now to be able to extend this you must extend both the widget and the state.
class ImplementedFooWidget extends FooWidget {
const ImplementedFooWidget({Key? key}) : super(key: key);
#override
_ImplementedFooWidgetState createState() => _ImplementedFooWidgetState();
}
class _ImplementedFooWidgetState extends _FooWidgetState {
#override
Widget build(BuildContext context) {
return Container();
}
}
Now let's look at using the generic (State<WidgetName>). If we use the generic, we can just define and extend the widget and have our own custom state.
abstract class BarWidget extends StatefulWidget {
const BarWidget({Key? key, required this.someRequiredString})
: super(key: key);
final String someRequiredString;
#override
State<BarWidget> createState();
}
class ImplementedBarWidget extends BarWidget {
const ImplementedBarWidget({Key? key, required String someRequiredString})
: super(
key: key,
someRequiredString: someRequiredString,
);
#override
_ImplementedBarWidgetState createState() => _ImplementedBarWidgetState();
}
class _ImplementedBarWidgetState extends State<ImplementedBarWidget> {
#override
Widget build(BuildContext context) {
return Container();
}
}
I hope that all makes sense.

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

pass one variables value to another variable in flutter

In my flutter app I created variables var from, to; and pass those with their values to the next page. which I used it with the widget.from & widget.to.
but how can i pass this widget.from & widget.to variables to var newFrom, newTo; these variables?
class FirstPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
var from = "Five", to = "Ten";
return SecondPage(from: from, to: to);
}
}
class SecondPage extends StatefulWidget {
final from, to;
const SecondPage({Key key, this.from, this.to}) : super(key: key);
#override
_SecondPageState createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {
var newFrom = "", newTo = "";
#override
void initState() {
setState(() {
newFrom = widget.from;
newTo = widget.to;
});
super.initState();
}
#override
Widget build(BuildContext context) {
return Container(
child: Text("$newFrom $newTo"),
);
}
}