1

I'm trying to build 2 screens one contains a list of products and the second has product details, I'm using Firebase to store data and so I fetch the data from Firestore in the first screen and it all works perfectly. However, in the second screen, I pass product data correctly using Navigator but I cannot use that data in my UI.

Here is the code for my second screen:

[!https://i.stack.imgur.com/BT1c9.png]

As you can see I cannot use the name variable in my widget tree, I've seen videos on youtube and I found that some people use state management to solve this problem, but for my case I just want to pass data and display it.

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50

2 Answers2

2

The way you've passed the data is correct but the way you've accessed the data isn't.

Whenever you try to access the variables of a Class in its state, you do that using widget. So, to access the name or other variables, just do as follows:

widget.name

Additionally, if the only purpose is to pass and display the data, then, there's no need of a StatefulWidget, use a StatelessWidget instead.

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50
  • 2
    To add to the above answer. If you use `StatelessWidget` instead, then your way of referring to the passed-in parameter is correct. For `StatefulWidget` arguments, you need to prefix each with `widget.`. – Roslan Amir Mar 16 '22 at 06:40
0

A [State] object's configuration is the corresponding [StatefulWidget] instance. This property is initialized by the framework before calling [initState]. If the parent updates this location in the tree to a new widget with the same [runtimeType] and [Widget.key] as the current configuration, the framework will update this property to refer to the new widget and then call [didUpdateWidget], passing the old configuration as an argument.

For that you can use widget.key, this will work fine for you.