0

I have the following in my analysis_options.yaml:-

analyzer:
  strong-mode:
    implicit-casts: false

When I use the json_serializable and generate to 'fromJson' method, and try to pass, the result from jsonDecode (from base dart::convert package), I get the error message "The argument type 'dynamic' can't be assigned to the parameter type 'Map'". The code runs and works but it's annoying to have these errors around my code, is there any way to keep the implicit converts off but stop this error from appearing?

Having used JsonSerializable the Person example they give in their documentation and trying to use the following call.

  factory Person.fromJsonString(String jsonString) {
    dynamic test = jsonDecode(jsonString);
    return Person.fromJson(test);
  }

jsonDecode is typed as dynamic even though it returns a Map<'String,dynamic'> Just seems like an odd point in the language.

Thanks in advance.

parkeradam
  • 11
  • 2
  • 1
    The complaint is about *implicit* casts. You always can use *explicit* casts instead (e.g. `someDynamicVariable as ActualType`). (Seeing the specific code that generates the analysis error might help.) As a last resort you can [exclude specific code from analysis by dartanalyzer](https://dart.dev/guides/language/analysis-options#excluding-code-from-analysis). – jamesdlin Mar 04 '20 at 07:00
  • I've added the code I was trying to use but you're right in that I can use the 'as' keyword to cast which fixes the issue. Just seems like a small flaw of the convert library to return dynamic when it doesn't. – parkeradam Mar 04 '20 at 08:50
  • `dart:convert`'s `jsonDecode` function must return a `dynamic` because it doesn't necessarily return a `Map`. It could return a `List` depending on what the JSON string represents. – jamesdlin Mar 04 '20 at 17:04
  • Ah, ok, I did not realise that. That's my bad. Appreciate the explanation :) – parkeradam Mar 05 '20 at 09:56

0 Answers0