0

I am pretty new in Dart and I would like to know, how to make Function defined as a property more type safe:

class NewTransaction extends StatelessWidget {

  final titleController = TextEditingController();
  final amountController = TextEditingController();
  final Function addNewTx;

  NewTransaction(this.addNewTx); 

With type safety I mean, that I can determine what are the inputs and outputs. Now I can pass anything to Function object.

softshipper
  • 32,463
  • 51
  • 192
  • 400

1 Answers1

2

When declaring a Function parameter, declare the return type and parameters.

In your example, I will imagine a void return type, and a String parameter. As a result:

class NewTransaction extends StatelessWidget {
  final titleController = TextEditingController();
  final amountController = TextEditingController();
  final void Function(String) addNewTx;

  NewTransaction(this.addNewTx);

}
AWhitford
  • 3,708
  • 3
  • 28
  • 38