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

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

Related

How to set default value of function in a constructor (Flutter)

I want to set default function in my child Widget in a constructor.
Basically, I have two widgets
Login (Parent Widget)
AppButton (Child Widget)
Here is my AppButton.dart
And I am calling this child widget in Login.dart (Parent) like this:
AppButton(title: "Login")
Please give me a way that to set default function without making "onPress" required for it's Parent (Login.dart)
TIA
Only static value can be set as default value in constructor, so you need define you function as static like this:
class AppButton extends StatefulWidget {
final Function onPress;
const AppButton({Key? key, this.onPress = _onPress}) : super(key: key);
static void _onPress(){}
#override
State<AppButton> createState() => _AppButtonState();
}
just make it nullable:
class MyButton extends StatefulWidget {
final void Function()? onPress;
final String title;
const MyButton({Key? key, this.onPress, required this.title}) : super(key: key);
#override
State<MyButton> createState() => _MyButtonState();
}
class _MyButtonState extends State<MyButton> {
void Function() defaultOnPress = (){
// your default function here
};
#override
Widget build(BuildContext context) {
return ElevatedButton(onPressed: widget.onPress ?? defaultOnPress, child: const Text("my button"));
}
}
still you can get const constructor
you could put "static" before childOnPress()

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 access State<T> from it's StatefulWidget

I'm writing a Application, where Widgets and their State need to be saved to Disk and later be restored. In order to save a StatefulWidget I need to access it's corresponding State<T> object.
Here's how I imagined the code to look like:
class Block extends StatefulWidget {
const Block({Key? key}) : super(key: key);
void saveToDisk(){
// access BlockState object
// save to disk…
}
#override
BlockState createState() => BlockState();
}
class BlockState extends State<Block> {
final String _someState = ‚Hello Stackoverflow‘;
#override
Widget build(BuildContext context) {
return const Text(‚Some Text‘);
}
}
Does anybody know how to access the BlockState object (first comment in saveToDisk())?
widget.saveToDisk();
All stateful widgets have it this way.

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 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