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

0

Issue

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?

Solution

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.

Answered By – Yeasin Sheikh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More