jeudi 24 septembre 2020

Override final properties in abstract class Dart

I created a Abstract Class with methods and properties that a Stateless Widget can override but after implementing the class, due to Stateless Widget immutability, I had to make the widget properties final and in turn make the Abstract class properties final as well. Then, I got error that the final variable must be initialized.

What is the proper way to implement an Abstract Class with final variables?

Abstract Class

abstract class BusinessCard {
  final CardView cardView;
  final CardInfo cardInfo;
  Widget front(BuildContext context);
  Widget back(BuildContext context);
}

Implementing Class

class Card1 extends StatelessWidget implements BusinessCard {
  final CardView cardView;
  final CardInfo cardInfo;

  Card1({
    @required this.cardView,
    @required this.cardInfo,
  });

  @override
  Widget build(BuildContext context) {
    return cardView == CardView.front ? front(context) : back(context);
  }

  @override
  Widget front(BuildContext context) {

  }

  @override
  Widget back(BuildContext context) {
    
  }
}

Aucun commentaire:

Enregistrer un commentaire