title: How to handle a null value in Flutter Model Factory
body: <p>I have a json that I need to parse and return to a <code>FutureBuilder</code>. The problem I have is in this line: <code>final meta = jsonDecode(parsedJson!['metadata']);</code> Sometimes <code>metadata</code> is null and it throws this error <code>Unhandled Exception: type 'Null' is not a subtype of type 'String'</code>. What is the best way to handle that?</p>
  <p>The metadata contains an image url that I want to use in a grid. So if possible, a default metadata object should be created with a default image url.</p>
  <pre><code>class Data {
    final int total;
    final MetaData metadata;
  
    Data({
      required this.total,
      required this.metadata,
    });
  
    factory Data.fromJson(parsedJson) {
      final meta = jsonDecode(parsedJson!['metadata']);
  
      MetaData md = MetaData.fromJson(meta);
  
      return Data(
        total: parsedJson['total'] ?? 0,
        metadata: md,
      );
    }
  }
  
  class MetaData {
    final String image;
  
    MetaData({
      required this.image,
    });
  
    factory MetaData.fromJson(Map&lt;String?, dynamic&gt; json) {
      return MetaData(
        image: json['image'] ?? &quot;&quot;,
      );
    }
  }
  </code></pre>
  <p>*** EDIT my solution</p>
  <p>I decided to do something like this. And I'll just populate the object string as needed. Would this be ok?</p>
  <pre><code>  factory Data.fromJson(parsedJson) {
      var metadataString = parsedJson['metadata'];
  
      metadataString ??=
          &quot;{\&quot;image\&quot;:\&quot;https://somewebsite.com/1.png\&quot;}&quot;;
  
      var meta = jsonDecode(metadataString);
  
      MetaData md = MetaData.fromJson(meta);
  
      return Data(
        total: parsedJson['total'] ?? 0,
        metadata: md,
      );
    }
  </code></pre>