Unable to get a default value on Select using the faunadb_http package

I am using the faunadb_http package and I want the value to be returned null (or any value) from Fauna DB if the field does not exist in the collection. I am just not able to figure out what should I put in the default parameter of this package so that I get that back as the default value.

I tried the following two variations of default parameter and I get “Value not found at path” error for first and just an empty Object {} for second.

'itemPrice': Select(["data", "itemPrice"], Var("postDoc"), default_: null),
'itemLocation': Select(["data", "itemLocation"], Var("postDoc"), default_: Obj({})),

Can somebody help me understand what should I be passing to default_ so that I get a String or Int as a response back.

This is the code for the Select class from the package

@JsonSerializable()
class Select extends Expr {
  @JsonKey(name: 'select')
  final Object path;

  final Expr from;

  @JsonKey(name: 'default', disallowNullValue: true, includeIfNull: false)
  final Expr? default_;

  Select(this.path, this.from, {this.default_});

  factory Select.fromJson(Map<String, dynamic> json) => _$SelectFromJson(json);

  @override
  Map<String, dynamic> toJson() => _$SelectToJson(this);
}

And this is for the Expr class

class Expr {
  static Object? wrap_value(dynamic value) {
    if (value is List) {
      return wrap_values(value);
    } else if (value is Map<String, dynamic>) {
      return Obj(value);
    } else if (value is DateTime) {
      return Time(value.toUtc().toIso8601String());
    } else {
      return value;
    }
  }

  static Object? wrap_values(Object? data) {
    if (data == null) return null;

    if (data is List) {
      return List.generate(
        data.length,
        (e) => wrap_value(data[e]),
        growable: false,
      );
    } else if (data is Map<String, dynamic>) {
      return data.map(
        (key, value) => MapEntry(key, wrap_value(value)),
      );
    }

    return data;
  }

  Expr();

  factory Expr.fromJson(Map<String, dynamic> json) => _$ExprFromJson(json);

  Map<String, dynamic> toJson() => _$ExprToJson(this);

  @override
  String toString() {
    return json.encode(this).toString();
  }
}

Hi @rahulrakesh - I’m going to set aside the language-specific aspects, as I’m not familiar with Dart.

That said, as I read through your post it seems like Select() is working as defined. The third argument is what is returned if your data is not found, e.g., null.

In the first case, you are returning null explicitly, and Fauna removes keys with null values, so that value would indeed not be found.

In the second case, you are returning an empty Object, and you receive an empty Object, so that seems to be working as defined as well.

Can somebody help me understand what should I be passing to default_ so that I get a String or Int as a response back.

In this case you need to explicitly set an Expr that will evaluate to a string or Int. If the empty string "" and zero 0 are reasonable defaults, then you would want:

'itemPrice': Select(["data", "itemPrice"], Var("postDoc"), default_: 0),

and

'itemLocation': Select(["data", "itemLocation"], Var("postDoc"), default_: ""),

Hi @rob thanks for giving it a look. I should have put in more examples to show what other things I tried.

I get these editor errors for what you have mentioned:

The argument type 'int' can't be assigned to the parameter type 'Expr?'
The argument type 'String' can't be assigned to the parameter type 'Expr?'.
1 Like

Ah, so it looks like a Dart type question (hence the tag). Unfortunately, I won’t be able to help you there.