Object runtimeType in Angular dart

Issue

I have a component which is provided a model object, this model object could be of Several types and what type it is determines what component will be rendered inside:

<div [ngSwitch]="poolModel.runtimeType.toString()">
    <template ngSwitchCase="CryptonoteMiningPool"><cryptonote-pool [model]="poolModel"></cryptonote-pool></template>
    <template ngSwitchCase="DaggerHashimotoMiningPool"><dag-hash-pool [model]="poolModel"></dag-hash-pool></template>
</div>

This works great in debug mode, but as soon as I compile for release runtimeType always returns “fS”.

I have a solution, essentially setting a constant in the model and looking at that, but would rather not have the hassle of that if I can avoid it since I might end up with many types of models to maintain.

Is there a way to make runtimeType return what I expect in release mode?

Solution

Runtime type isn’t something I’d use for program logic. It changes based on compiler options, and actually even using it can make optimizations for your app (in dart2js) much harder.

We ban using it in our dart2js performance guide.

You’re probably better off creating some sort of base class:

abstract class DynamicRender {
  String get renderType;
}

And having your classes extend/mix/implement and use that instead.

Answered By – matanlurey

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.