skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. instead of using as you can use .toString()

      final dynamic test = null.toString();
    

    While also using variable,

      final String data = "${model.filed}";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search