7
7
8
8
import 'dart:async' ;
9
9
import 'dart:js_interop' ;
10
+ import 'dart:js_interop_unsafe' ;
10
11
11
12
import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart'
12
13
as platform_interface;
13
14
import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart' ;
14
15
import 'package:cloud_firestore_web/src/utils/encode_utility.dart' ;
15
16
import 'package:firebase_core/firebase_core.dart' ;
16
- import 'package:firebase_core_web/firebase_core_web_interop.dart'
17
- hide jsify, dartify;
17
+ import 'package:firebase_core_web/firebase_core_web_interop.dart' ;
18
18
import 'package:flutter/foundation.dart' ;
19
19
20
20
import 'firestore_interop.dart' as firestore_interop;
@@ -275,8 +275,7 @@ class LoadBundleTaskProgress
275
275
final int totalDocuments;
276
276
}
277
277
278
- class WriteBatch extends JsObjectWrapper <firestore_interop.WriteBatchJsImpl >
279
- with _Updatable {
278
+ class WriteBatch extends JsObjectWrapper <firestore_interop.WriteBatchJsImpl > {
280
279
static final _expando = Expando <WriteBatch >();
281
280
282
281
/// Creates a new WriteBatch from a [jsObject] .
@@ -302,12 +301,12 @@ class WriteBatch extends JsObjectWrapper<firestore_interop.WriteBatchJsImpl>
302
301
303
302
WriteBatch update (DocumentReference documentRef, Map <String , dynamic > data) =>
304
303
WriteBatch .getInstance (
305
- _wrapUpdateFunctionCall (jsObject, data, documentRef));
304
+ jsObject.update (documentRef.jsObject, jsify (data)! as JSObject ),
305
+ );
306
306
}
307
307
308
308
class DocumentReference
309
- extends JsObjectWrapper <firestore_interop.DocumentReferenceJsImpl >
310
- with _Updatable {
309
+ extends JsObjectWrapper <firestore_interop.DocumentReferenceJsImpl > {
311
310
static final _expando = Expando <DocumentReference >();
312
311
313
312
/// Non-null [Firestore] the document is in.
@@ -395,24 +394,25 @@ class DocumentReference
395
394
}
396
395
397
396
Future <void > set (Map <String , dynamic > data,
398
- [firestore_interop.SetOptions ? options]) {
399
- var jsObjectSet = (options != null )
400
- ? firestore_interop.setDoc (jsObject, jsify (data)! as JSObject , options)
401
- : firestore_interop. setDoc (jsObject, jsify (data) ! as JSObject ) ;
402
-
403
- return jsObjectSet .toDart;
397
+ [firestore_interop.SetOptions ? options]) async {
398
+ if (options != null ) {
399
+ await firestore_interop.setDoc (jsObject, jsify (data), options).toDart;
400
+ return ;
401
+ }
402
+ await firestore_interop. setDoc (jsObject, jsify (data)) .toDart;
404
403
}
405
404
406
- Future <void > update (Map <firestore_interop.FieldPath , dynamic > data) {
407
- final alternatingFieldValues = data.keys
405
+ Future <void > update (Map <firestore_interop.FieldPath , dynamic > data) async {
406
+ final List < JSAny ?> alternatingFieldValues = data.keys
408
407
.map ((e) => [jsify (e), jsify (data[e])])
409
408
.expand ((e) => e)
410
409
.toList ();
411
410
412
- return handleThenable (callMethod (firestore_interop.updateDoc, 'apply' , [
411
+ await firestore_interop.updateDoc
412
+ .callMethodVarArgs <JSPromise >('apply' .toJS, [
413
413
null ,
414
- [jsObject, ...alternatingFieldValues]
415
- ])) ;
414
+ [jsObject, ...alternatingFieldValues]. jsify ()
415
+ ]).toDart ;
416
416
}
417
417
}
418
418
@@ -503,16 +503,28 @@ class Query<T extends firestore_interop.QueryJsImpl>
503
503
}
504
504
505
505
Query startAfter ({DocumentSnapshot ? snapshot, List <dynamic >? fieldValues}) =>
506
- Query .fromJsObject (firestore_interop.query (
506
+ Query .fromJsObject (
507
+ firestore_interop.query (
507
508
jsObject,
508
509
_createQueryConstraint (
509
- firestore_interop.startAfter, snapshot, fieldValues)));
510
+ firestore_interop.startAfter,
511
+ snapshot,
512
+ fieldValues,
513
+ ),
514
+ ),
515
+ );
510
516
511
517
Query startAt ({DocumentSnapshot ? snapshot, List <dynamic >? fieldValues}) =>
512
- Query .fromJsObject (firestore_interop.query (
518
+ Query .fromJsObject (
519
+ firestore_interop.query (
513
520
jsObject,
514
521
_createQueryConstraint (
515
- firestore_interop.startAt, snapshot, fieldValues)));
522
+ firestore_interop.startAt,
523
+ snapshot,
524
+ fieldValues,
525
+ ),
526
+ ),
527
+ );
516
528
517
529
Query where (dynamic fieldPath, String opStr, dynamic value) =>
518
530
Query .fromJsObject (
@@ -530,18 +542,24 @@ class Query<T extends firestore_interop.QueryJsImpl>
530
542
/// [fieldValues] .
531
543
/// We need to call this method in all paginating methods to fix that Dart
532
544
/// doesn't support varargs - we need to use [List] to call js function.
533
- S ? _createQueryConstraint <S >(
545
+ firestore_interop. QueryConstraintJsImpl _createQueryConstraint <S >(
534
546
Object method, DocumentSnapshot ? snapshot, List <dynamic >? fieldValues) {
535
547
if (snapshot == null && fieldValues == null ) {
536
548
throw ArgumentError (
537
549
'Please provide either snapshot or fieldValues parameter.' );
538
550
}
539
551
540
- var args = (snapshot != null )
552
+ final args = (snapshot != null )
541
553
? [snapshot.jsObject]
542
554
: fieldValues! .map (jsify).toList ();
543
555
544
- return callMethod (method, 'apply' , [null , jsify (args)]);
556
+ return (method as JSObject ).callMethodVarArgs <JSAny >(
557
+ 'apply' .toJS,
558
+ [
559
+ null ,
560
+ jsify (args).jsify (),
561
+ ],
562
+ ) as firestore_interop.QueryConstraintJsImpl ;
545
563
}
546
564
547
565
Object _parseFilterWith (Map <String , Object ?> map) {
@@ -566,9 +584,21 @@ class Query<T extends firestore_interop.QueryJsImpl>
566
584
}
567
585
568
586
if (opStr == 'OR' ) {
569
- return callMethod (firestore_interop.or, 'apply' , [null , jsFilters]);
587
+ return firestore_interop.or.callMethodVarArgs <JSAny >(
588
+ 'apply' .toJS,
589
+ [
590
+ null ,
591
+ jsFilters.jsify (),
592
+ ],
593
+ );
570
594
} else if (opStr == 'AND' ) {
571
- return callMethod (firestore_interop.and, 'apply' , [null , jsFilters]);
595
+ return firestore_interop.and.callMethodVarArgs <JSAny >(
596
+ 'apply' .toJS,
597
+ [
598
+ null ,
599
+ jsFilters.jsify (),
600
+ ],
601
+ );
572
602
}
573
603
574
604
throw Exception ('InvalidOperator' );
@@ -747,8 +777,7 @@ class QuerySnapshot
747
777
.toDart;
748
778
}
749
779
750
- class Transaction extends JsObjectWrapper <firestore_interop.TransactionJsImpl >
751
- with _Updatable {
780
+ class Transaction extends JsObjectWrapper <firestore_interop.TransactionJsImpl > {
752
781
static final _expando = Expando <Transaction >();
753
782
754
783
/// Creates a new Transaction from a [jsObject] .
@@ -779,24 +808,8 @@ class Transaction extends JsObjectWrapper<firestore_interop.TransactionJsImpl>
779
808
Transaction update (
780
809
DocumentReference documentRef, Map <String , dynamic > data) =>
781
810
Transaction .getInstance (
782
- _wrapUpdateFunctionCall (jsObject, data, documentRef));
783
- }
784
-
785
- /// Mixin class for all classes with the [update()] method. We need to call
786
- /// [_wrapUpdateFunctionCall()] in all [update()] methods to fix that Dart
787
- /// doesn't support varargs - we need to use [List] to call js function.
788
- mixin _Updatable {
789
- /// Calls js [:update():] method on [jsObject] with [data] or list of
790
- /// [fieldsAndValues] and optionally [documentRef] .
791
- T ? _wrapUpdateFunctionCall <T >(jsObject, Map <String , dynamic > data,
792
- [DocumentReference ? documentRef]) {
793
- var args = [jsify (data)];
794
- // documentRef has to be the first parameter in list of args
795
- if (documentRef != null ) {
796
- args.insert (0 , documentRef.jsObject as JSObject );
797
- }
798
- return callMethod (jsObject, 'update' , args);
799
- }
811
+ jsObject.update (documentRef.jsObject, jsify (data)! ),
812
+ );
800
813
}
801
814
802
815
class _FieldValueDelete implements FieldValue {
@@ -826,9 +839,13 @@ class _FieldValueArrayUnion extends _FieldValueArray {
826
839
827
840
@override
828
841
firestore_interop.FieldValue ? _jsify () {
829
- // This uses var arg so cannot use js package
830
- return callMethod (
831
- firestore_interop.arrayUnion, 'apply' , [null , jsify (elements)]);
842
+ return firestore_interop.arrayUnion.callMethodVarArgs <JSAny >(
843
+ 'apply' .toJS,
844
+ [
845
+ null ,
846
+ jsify (elements),
847
+ ],
848
+ ) as firestore_interop.FieldValue ;
832
849
}
833
850
834
851
@override
@@ -840,9 +857,13 @@ class _FieldValueArrayRemove extends _FieldValueArray {
840
857
841
858
@override
842
859
firestore_interop.FieldValue ? _jsify () {
843
- // This uses var arg so cannot use js package
844
- return callMethod (
845
- firestore_interop.arrayRemove, 'apply' , [null , jsify (elements)]);
860
+ return firestore_interop.arrayRemove.callMethodVarArgs <JSAny >(
861
+ 'apply' .toJS,
862
+ [
863
+ null ,
864
+ jsify (elements),
865
+ ],
866
+ ) as firestore_interop.FieldValue ;
846
867
}
847
868
848
869
@override
0 commit comments