let’s say I have a class that has a particular style and placement for 2 buttons. I need the style and placement of buttons to be the same across some aspects of the app. As a result a programmer can add a text and the onTap function of the primary button, and if UI contains a second button programmer can add that as well.
class ButtonPlacement extends StatelessWidget {
const ButtonPlacement({required this.primaryButtonText,required this.primaryButtonOnTap,
this.secondaryButtonText,this.secondaryButtonOnTap, super.key});
etc
My question is, is there a way to throw a compile error just like in null safety required, if programmer adds lets say a secondary text but not a secondary onTap function, as these two go together as you understand.
3
Answers
I just created a new class with these two variables, so programmer should input at most two of these classes instead of 4 variables. I will keep question open as I am intrigued to see other solutions
You can use
assert
which will only work in development.So if you try to create an instance of
ButtonPlacement
with one asserted parameter provided but not the other, it will throw and error in the console when you run it.you can use ErrorWidget on debug mode and even better, Bug report on release if you want. you can even write your own message in way the other team will understand maybe native language.
the simplest way is :
in your case you can check :