softshipper
softshipper

Reputation: 34061

How to make the function type safe?

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.

Upvotes: 0

Views: 129

Answers (1)

AWhitford
AWhitford

Reputation: 3998

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);

}

Upvotes: 2

Related Questions