最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - How to get realtime updates with getx - Stack Overflow

programmeradmin1浏览0评论

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()));
    }
  }

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()));
    }
  }
Share Improve this question edited yesterday Frank van Puffelen 599k85 gold badges888 silver badges858 bronze badges Recognized by Google Cloud Collective asked yesterday Abdirahman AbdullahiAbdirahman Abdullahi 11 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 0

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

[]helo1

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),
    );
  }
}
发布评论

评论列表(0)

  1. 暂无评论