rrousselGit / functional_widget

A code generator to write widgets as function without loosing the benefits of classes.
597 stars 46 forks source link

Support [assert(...)]of some parameters #65

Closed pedromassango closed 3 years ago

pedromassango commented 4 years ago

Does declaring a widget as a function support parameters's assert?

Most of the times I writ my code as:

class MyWidget extends StatelessWidget {
  final int valueA;
  final int valueB;

  MyWidget({this.valueA, this.valueB})
    : assert(valueA != null),
      assert(valueB != 1);

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

How do I write the assert so that this package can apply in the generated widget? If I write it in the fuction it will be moved in the generated widget as well?

rrousselGit commented 4 years ago

It is really needed? The assert can be placed directly inside the function:

@swidget
Widget example(int value) {
  assert(value > 0);
  return Container();
}
pedromassango commented 4 years ago

My doubt is if the assert will be called since we will use the generated widget istead of the function. Yes it is needed sometimes I need to put some restrictions in the provided parameters.

rrousselGit commented 4 years ago

The generated widget calls your function, so asserts will be called correctly

rrousselGit commented 3 years ago

Closing since I believe supporting asserts on the constructor when we can place asserts inside the function has a low benefit

I'm open to pull-requests though