Isak
Isak

Reputation: 578

Specify type for dynamic variable in named constructor

Consider the following class.

class Test {
  final String foo;
  final dynamic bar;

  Test({this.foo, this.bar});

  Test.barInt({this.foo, this.bar});
}

How would the field bar be speified as an int in the named constructur barInt?

Upvotes: 0

Views: 78

Answers (1)

Mattia
Mattia

Reputation: 6524

You can just specify the type before the parameter:

Test.barInt({this.foo, int this.bar});

be aware that later in the code it is possible to change the value of bar to any type from its setter. You might want to check out generics for this use case.

Upvotes: 1

Related Questions