firebase-functions-interop icon indicating copy to clipboard operation
firebase-functions-interop copied to clipboard

TransactionResult doesn't allow abort on non dynamic data

Open natebot13 opened this issue 5 years ago • 1 comments
trafficstars

await database.ref('queue').transaction<Map<String, dynamic>>(
  (currentData) {
    print(currentData);
    if (currentData.length.isOdd) {
      print('aborting!');
      return TransactionResult.abort;
    }
    print('hand waving done');
    return TransactionResult.success(currentData);
  },
);

I'm running into a compile issue with this code. The transaction.abort line gives an error saying: The return type 'TransactionResult<dynamic>' isn't a 'TransactionResult<Map<String, dynamic>>', as required by the closure's context.

Am I allowed to specify the transaction type?

natebot13 avatar Sep 19 '20 03:09 natebot13

This looks like a bug in the admin interop package:

https://github.com/pulyaevskiy/firebase-admin-interop/blob/b958afb5cdf893ce76b2d71c67ee4b023e5087d4/lib/src/database.dart#L560-L568

class TransactionResult<T> {
  TransactionResult._(this.aborted, this.data);
  final bool aborted;
  final T data;


  static TransactionResult abort = new TransactionResult._(true, null);
  static TransactionResult<T> success<T>(T data) =>
      new TransactionResult._(false, data);
}

The abort field should really be closer to

static TransactionResult<T> abort = new TransactionResult<T>._(true, null);

Though I'm not sure if this is a valid use of generics. Might need to be changed into a static method the same way as success.

pulyaevskiy avatar Sep 23 '20 04:09 pulyaevskiy