How to get realtime updates with getx

I am fairly new to flutter. I am using Getx for state management.What is the best way to listen to firestore realtime updates? I use the RxListin an obx in the ui to build items using a listviewBuilder.

RxList<PayClientModel> payments = <PayClientModel>[].obs;
RxList<ReceiveModel> receipts = <ReceiveModel>[].obs;
RxList<ExpenseModel> expenses = <ExpenseModel>[].obs;

final withdrawalList = <WithdrawModel>[];
RxList<WithdrawModel> withdrawals = <WithdrawModel>[].obs;
FirebaseFirestore.instance
    .collection('Users')
    .doc(FirebaseAuth.instance.currentUser!.uid)
    .collection('transactions')
    .snapshots()
    .listen((querySnapshot) {
  // expenses.clear();
  payments.clear();
  receipts.clear();
  withdrawals.clear();
  for (var doc in querySnapshot.docs) {
    final transactionType = doc.data()['transactionType'];
    if (doc.exists && transactionType == 'expense') {
      expenses.add(ExpenseModel.fromJson(doc.data()));
      print(expenses.length);
    } else if (transactionType == 'payment') {
      payments.add(PayClientModel.fromJson(doc.data()));
    } else if (transactionType == 'receipt') {
      receipts.add(ReceiveModel.fromJson(doc.data()));
    } else if (transactionType == 'withdraw') {
      withdrawals.add(WithdrawModel.fromJson(doc.data()));
    }
  }

Upvotes: 0

Views: 59

Answers (2)

user29587781
user29587781

Reputation: 1

Need assistance with a continuous bugging of something similar to this. I have noticed this trend of things missing and popping back into phone. I have restored with apple almost 4 times this year (today would be #4). I came across emoticon and pho cloud malware information very similar to of reading of these analytics. Please lmk how to get rid of enter image description here

Image

Upvotes: 0

Prem Patel
Prem Patel

Reputation: 1

class HomeController extends GetxController {
  var count = 0.obs;
  void increment() => count++;
}

class HomePage extends StatelessWidget {
  final controller = Get.put(HomeController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: Obx(() => Text("${controller.count}"))),
      floatingActionButton: FloatingActionButton(
        onPressed: controller.increment),
    );
  }
}

Upvotes: 0

Related Questions