dart – A value of type String cannot be assigned to a variable of type Hero

dart – A value of type String cannot be assigned to a variable of type Hero

The problem seems to be in this line:

Hero hero = Windstorm;

hero is of type Hero.
Windstorm is of type String.
So one cannot be assigned to the other just like that.

You can try this:

Hero hero = new Hero(Windstorm);

Just like you did in your last line with test.

If there is a constructor for the class Hero that takes a string as an argument, then it will work.

Here sample,

export class AppComponent  {

 hero = new Hero(windstrom);
}

export class Hero 
{
    name: string;

    constructor(Name: string){
    this.name =Name;
    }
}

let hero = new Hero(test);

Its works

dart – A value of type String cannot be assigned to a variable of type Hero

Leave a Reply

Your email address will not be published. Required fields are marked *