Adhitya Resadikara
Adhitya Resadikara

Reputation: 117

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?

Upvotes: 0

Views: 106

Answers (1)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63669

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.

Upvotes: 1

Related Questions