aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.33k stars 248 forks source link

Datastore: Support DateTime in Contains Query Predicate #3566

Open Lance-Kraakman opened 1 year ago

Lance-Kraakman commented 1 year ago

Description

Doing a simple observeQuery, with a TepmoralDateTime string

  final qry = date.toUtc().toTemporalDateTime().toString().substring(0, 10);
  final awStream = Amplify.DataStore.observeQuery(AthleteWorkout.classType,
      where: AthleteWorkout.STARTTIME.contains(qry.toString()));

Is throwing this error.

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: QueryException { E/flutter (13707): "message": "Invalid type. The .contains() query predicate only supports type String and List.", E/flutter (13707): "recoverySuggestion": "Ensure that the field is of the appropriate type." E/flutter (13707): }

image

If I change evaluate (in query_field_operators.dart) to:

  @override
  bool evaluate(dynamic other) {
    if (other == null) {
      return false;
    } else if (other is String) {
      return other.contains(value);
    } else if (other is List<String>) {
      return other.contains(value);
    } else if ((other is TemporalDateTime) || (other is TemporalDate)) {
      String otherString = other.toString();
      String valueString = value.toString();
      return otherString.contains(valueString);
    } else {
      throw const ModelQueryException(
        'Invalid type. The .contains() query predicate only supports type String and List<String>.',
        recoverySuggestion: 'Ensure that the field is of the appropriate type.',
      );
    }
  }

It works fine. Although there will likely be a more elegant solution. Any suggestions?

Thanks

Categories

Steps to Reproduce

Doing a simple observeQuery, with a TepmoralDateTime query string in the 'contains' query.

Platforms

Flutter Version

3.13

Amplify Flutter Version

1.3.2

Deployment Method

Amplify CLI

Schema

No response

fjnoyp commented 1 year ago

Hi @Lance-Kraakman thanks for opening this issue! Glad to see you've found a solution to the type error for dateTime.

Your solution looks good. I will mark this as a feature request for Amplify Flutter Datastore to officially support QueryPredicate for DateTime but the code you wrote should work for your case in the meantime.