Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arrayUnion (Add field in array) #85

Open
haleem13 opened this issue Mar 25, 2022 · 1 comment
Open

arrayUnion (Add field in array) #85

haleem13 opened this issue Mar 25, 2022 · 1 comment

Comments

@haleem13
Copy link

haleem13 commented Mar 25, 2022

Can anyone please help me to Add field in array????
Firestore.instance .collection('YourCollection') .document('YourDocument') .updateData({'array':FieldValue.arrayUnion(['data1','data2','data3'])});

@Defuera
Copy link

Defuera commented Nov 10, 2023

Here's an extension method that you can use for this purpose created by me and some LLM model

import 'package:firedart/firedart.dart';

extension DocumentReferenceExtension on DocumentReference {
  Future<void> updateArrayField(String fieldName, dynamic item) async {
    if (!(await this.exists)) {
      await this.set({fieldName: [item]});
    } else {
      final doc = await this.get();
      final currentItems = doc[fieldName] as List<dynamic>? ?? [];
      await this.update({fieldName: [...currentItems, item]});
    }
  }
}

usage

    final docRef = collection.document(docId);
    await docRef.updateArrayField('items', json);

mostly by some LLM model...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants