Casting null as a String is allowed in my local flutter environment during debugging. However, when deployed to production, the same typecast is treated as a crashing exception.
The code can be translated to the following:
final dynamic test = null as String;
print(test);
The actual code is generated by the json_serializable package (https://pub.dev/packages/json_serializable), thus alternating the code itself is not possible.
My local environment described through flutter doctor:
Local flutter environment
The production crash can be simulated in Dartpad https://dartpad.dev/?channel=old
What differs the environments? I’d like the development environment to throw the error, so it’s spotted & fixed before being pushed to production state.
I’ve tried comparing the production environment with the development environment, but found no major differentiations that could explain the behaviour.
2
Answers
As jamesdlin pointed out in the comments, the client should not trust the server to provide reliable data. Implementing client side null-checks and fallback makes the client more resilient.
Hence all fields declared using json_serializable shuold be marked as nullable.
instead of using
as
you can use.toString()
While also using variable,