Home > Mobile >  Flutter: live stream with getx has a problem, it shows data without updating
Flutter: live stream with getx has a problem, it shows data without updating

Time:10-06

I use Getx package in my apps, it works beautiful, but when I tried to get the data from firestore in stream it gets the data but without stream, so my UI hasn't updated until I go to another page and reenter the page which has the data!!!

Actually it's a big problem to bring this data without streaming, each time I have to exit and reopen to update!!!

Here my controller class:

class AddSubscriberController extends GetxController {

final CollectionReference _subscribersCollectionRef =
      FirebaseFirestore.instance.collection('Subscribers');

List<SubscriberModel> get subscriberModel => _subscriberModel;
  final List<SubscriberModel> _subscriberModel = [];
  RxList<SubscriberModel> subscribers = RxList([]);


  @override
  void onInit() {
    super.onInit();
    subscribers.bindStream(getSubscribers());
  }


  Stream<List<SubscriberModel>> getSubscribers() {
    return _subscribersCollectionRef
        .orderBy('Register date', descending: false)
        .snapshots()
        .map((QuerySnapshot query) {
      for (var element in query.docs) {
        subscriberModel.add(SubscriberModel.fromMap(element));
      }
      return subscriberModel;
    });
  }
}

And here is my UI class:

Obx(() {
          AddSubscriberController controller =
              Get.put(AddSubscriberController());
          return ListView.builder(
            physics: const BouncingScrollPhysics(),
            shrinkWrap: true,
            scrollDirection: Axis.vertical,
            itemCount: controller.subscribers.length,
            itemBuilder: (context, index) {
              return buildSubInfo(
                name: controller.subscribers[index].firstName,
                lastName: controller.subscribers[index].lastName,
                father: controller.subscribers[index].father,
                area: controller.subscribers[index].area,
                counterNo: controller.subscribers[index].counterNumber,
                date: controller.subscribers[index].date,
              );
            },
          );
        }), // Obx

How can I solve this??

Any help will be appreciated.

CodePudding user response:

Can you try this:

GetX<AddSubscriberController>(builder : (controller) {         
          return ListView.builder(
            physics: const BouncingScrollPhysics(),
            shrinkWrap: true,
            scrollDirection: Axis.vertical,
            itemCount: controller.subscribers.length,
            itemBuilder: (context, index) {
              return buildSubInfo(
                name: controller.subscribers[index].firstName,
                lastName: controller.subscribers[index].lastName,
                father: controller.subscribers[index].father,
                area: controller.subscribers[index].area,
                counterNo: controller.subscribers[index].counterNumber,
                date: controller.subscribers[index].date,
              );
            },
          );
        }),

CodePudding user response:

class AddSubscriberController extends GetxController {

final CollectionReference _subscribersCollectionRef =
      FirebaseFirestore.instance.collection('Subscribers');


  final List<SubscriberModel> subscriberModel = <SubscriberModel>[].obs;


 @override
  void onInit() {
    getSubscribers();
    super.onInit();
    
  }


   getSubscribers() {
    return _subscribersCollectionRef
        .orderBy('Register date', descending: false)
        .snapshots()
        .listen((event) {
         //// try to simple approach 
        for (var x in event.docs) {
           final Map<String, dynamic> y = x.data();
           final tojson = SubscriberModel.fromJson(y);
           subscriberModel.add(tojson); 
      }
      
    });
  }
}

for the view

class View extends StatelessWidget {
  const View ({super.key});

 @override
  Widget build(BuildContext context) {
   final controller =  Get.put(AddSubscriberController());

  return Obx(
()=> Scaffold(
  body : ListView.builder(
            physics: const BouncingScrollPhysics(),
            shrinkWrap: true,
            scrollDirection: Axis.vertical,
            itemCount: controller.subscriberModel.length,
            itemBuilder: (context, index) {
             final item = controller.subscriberModel[index];
              return buildSubInfo(
                name: item.firstName,
                lastName: item.lastName,
                father:item.father,
                area: item.area,
                counterNo: item.counterNumber,
                date: item.date,
              );
            },
          ),
));
}

try this if this work

  • Related