passsy / dart-lint

An opinionated, community-driven set of lint rules for Dart and Flutter projects. Like pedantic but stricter
Apache License 2.0
277 stars 82 forks source link

invalid_assignment rule #15

Closed Storch72 closed 3 years ago

Storch72 commented 4 years ago

Hello. I have tried several different ways to get the linter to ignore this error but am unsure what the correct answer is.

"A value of type ‘{0}’ can’t be assigned to a variable of type ‘{1}’" I have tried:

   invalid_assignment: true
   invalid_assignment: false
   invalid_assignment: ignore
   invalid_assignment: warn

Nothing seems to prevent this from being a compile error in my project except removing [lint: ^1.0.0] completely What am I doing wrong? What do I need to do to use?

Storch72 commented 4 years ago

Okaay...

ChristianConstantNel commented 3 years ago

@Storch72 I'm also having the same problem for all my map functions in my classes. Did you manage to fix them?

Storch72 commented 3 years ago

No, never did and no one ever responded tp my question. This feels like nobody looks at this anymore. :-(

ChristianConstantNel commented 3 years ago

Ahh damn. I'm really excited to use this in my projects, but I have no idea how to fix the assignment issue. Maybe someone will respond with some tips.

ChristianConstantNel commented 3 years ago

@Storch72 I think I found a solution.

Just after your map function type cast the variable to whatever it needs to be using as. For example: bestTry: map['bestTry'] as double

Hope this helps! Don't know if this is the correct way of solving the issue though.

passsy commented 3 years ago

Changing the warning level, doesn't remove the warning, it only changes the level which has basically no effect in any tooling.

To ignore certain rules edit your pubpsec.yaml

include: package:lint/analysis_options.yaml

linter:
  rules:
    invalid_assignment: false
ChristianConstantNel commented 3 years ago

@passsy I've tried this, but it still shows up for all my map functions.

ChristianConstantNel commented 3 years ago

Can it be that my map functions are doing something wrong? For example:

`factory ChessStats.fromMap(Map<String, dynamic> map) { if (map == null) return null;

return ChessStats(
  gamesPlayed: map['gamesPlayed'], //This is where I get the error
  gamesWon: map['gamesWon'],
  gamesDrawn: map['gamesDrawn'],
  gamesLost: map['gamesLost'],
);

}`

passsy commented 3 years ago

map['gamesPlayed'] returns dynamic. which is not bool. You definitely have to add the cast to be correct.

The reason you see this error is because of implicit-cast: false. Without lint implicit-cast is set to true.

I really, really recommend disabling implicit-casts as they otherwise cause errors at runtime which could be caught at compile time. But you can enable them again:

include: package:lint/analysis_options.yaml

analyzer:
  strong-mode:
    implicit-casts: true
ChristianConstantNel commented 3 years ago

Thank you for clarifying this. I'd rather cast all my attributes to catch these errors on compile time.

Have a nice day/night.