Passing a value through constructor in dart

Viewed 77

I am passing a value in my product page but it is coming as null even when the value that I am passing is not null.

onTap: () {
          setState(() {
             ProductListPage(uid: arguments["userId"],);
                 print(arguments["userId"]);
          });

This is the code for product page

const ProductListPage({Key? key, this.uid}) : super(key: key);
final uid;

@override
  _ProductListPageState createState() => _ProductListPageState();
}

class _ProductListPageState extends State<ProductListPage>
void getProducts() async {
print("new id");
print(widget.uid);
}
1 Answers

If you are trying to setState outside ProductListPage (which you probably are), it won't work that way.

If whatever you are pressing is passed from ProductListPage, you would pass a setState function to that widget from ProductListPage, so you can call that function to setState in ProductListPage when that widget is pressed.

In your case, your setState is only happening in the stateful widget that is being pressed.

If your button isn't passed from ProductListPage, you'll need to use a state management solution like the Provider Package or RiverPod.

Related