4

In Javascript, to conditionally add a value to an Object, I can do something like this:

const obj = {
   ...(myCondition && {someKey: "someValue"})
}

Can I do something similar to pass in a named parameter in Dart / Flutter? For example, if I have the below code, is there a way to conditionally pass the place parameter only if it exists in the json passed into the fromJson factory function.

factory SearchResult.fromJson(Map<String, dynamic> json) {
    return SearchResult(
      id: json['id'],
      displayString: json['displayString'],
      name: json['name'],
      recordType: json['recordType'],
      collection: json['collection'],
      place: GeoJson.fromJson(json['place']),
      properties: json['properties']
    );
  }
3
  • Is not passing value to named parameter different from passing null instead? Because I usually do something like place: GeoJson.fromJson(json['place']) ?? null Commented Feb 5, 2020 at 3:49
  • @FederickJonathan that's a good point. The problem that I face is that if I pass null, the GeoJson.fromJson method throws an error because it expects a value. Is there a way to allow a value to be a String or null kind of like in Typescript you can say private someAttribute: string | null. Commented Feb 5, 2020 at 11:19
  • In that case, you could do bool isPlaceNotEmpty = json['place'] != null. Then place: isPlaceNotEmpty ? GeoJson.fromJson(json['place']) : null. You won't call fromJson and passing null value this way Commented Feb 5, 2020 at 16:35

3 Answers 3

4

You might be looking for Dart's collection operators, specifically collection-if and collection-for capabilities.

You can, for example do something like:

final map = {
  'key1': 'value1',
  'key2': 'value2',
  if (myCondition) 'key3': 'value3'
};

This works in lists, too:

final list = ['value1', 'value2', if (myCondition) 'value3']; 

In this case, you might be after something along the lines of:

final keys = [
  'id',
  'displayString',
  'name',
  'recordType',
  'collection',
  'place',
  'properties'
],
obj = {for (final key in keys) if (json.containsKey(key)) key: json[key]};
Sign up to request clarification or add additional context in comments.

1 Comment

That's for sure helpful. Though, I am trying to figure out a way to conditionally (if it exists) pass a parameter to the fromJson method above rather than add it to an object. Seems my only option might be to handle the null when passing values in.
0
factory SearchResult.fromJson(Map<String, dynamic> json) {
    var isNotEmpty = json['place'] != null;

    return SearchResult(
      id: json['id'],
      displayString: json['displayString'],
      name: json['name'],
      recordType: json['recordType'],
      collection: json['collection'],
      /// This way you won't call fromJson and passing null if json['place'] is null in the first place
      place: isNotEmpty ? GeoJson.fromJson(json['place']) : null,
      properties: json['properties']
    );
  }

Comments

0

In dart if you receive a null object you can use double question mark operator:

  • ??

What it means? : If object is null then take what it is after double question mark

var b = null;
var a = b ?? 'b was null, you got me!';
print(a);

result:

a: b was null, you got me!

For example:

factory SearchResult.fromJson(Map<String, dynamic> json) {
    var isNotEmpty = json['place'] != null;

    return SearchResult(
      id: json['id'] ?? 0,
      displayString: json['displayString'] ?? '',
      name: json['name'] ?? '',
      recordType: json['recordType'] ?? '',
      collection: json['collection'] ? '',
      place: GeoJson.fromJson(json['place'] ?? {}),
      properties: json['properties'] ?? [],
    );
  }

Null safe types are fun to use

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.