0

I have a payment list that I need to import from a subcollection included in a payments collection and display it in real time. I use the stream to get a real-time change in the display of my data obtained from the firebase. Unfortunately, it didn't work.

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
import 'package:line_icons/line_icons.dart';
import 'package:pharma_smart/common/globals.dart';
import 'package:pharma_smart/common/utils/date_picker.dart';
import 'package:pharma_smart/common/utils/utils.dart';
import 'package:pharma_smart/invoice_management/amount_to_be_paid.dart';
import 'package:pharma_smart/invoice_management/invoice.dart';
import 'package:pharma_smart/invoice_management/invoices_list.dart';

class BillPayment extends StatefulWidget {
  final String invoiceId;
  final List<InvoicePayment> billPayments;
  final int amountInvoice;

  const BillPayment(this.invoiceId, this.amountInvoice,
      {Key? key, required this.billPayments})
      : super(key: key);

  @override
  State<BillPayment> createState() => _BillPaymentState();
}

class _BillPaymentState extends State<BillPayment> {
  TextEditingController amountPaidController = TextEditingController();
  String dropdownValue = '';
  TextEditingController docNumController = TextEditingController();
  TextEditingController cashedDateController = TextEditingController();
  TextEditingController handedDateController = TextEditingController();
  TextEditingController dueDateController = TextEditingController();
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  final CollectionReference collection =
     FirebaseFirestore.instance.collection('detailFactue');
 List<InvoicePayment> billPayments = [];
 bool isLoading = false;
 bool toAdd = false;

      void initState() {
        super.initState();
  }

  

  @override
  Widget build(BuildContext context) {
    double _w = MediaQuery.of(context).size.width;
    return Scaffold(
        appBar: AppBar(),
        body: Container(
                child: StreamBuilder<QuerySnapshot>(
                stream: collection
                   .doc(widget.invoiceId)
                    .collection('payments')
                    .snapshots(),
                builder: (context, dataSnapshot) {
                  if (dataSnapshot.connectionState == ConnectionState.waiting) {
                    return const Center(child: CircularProgressIndicator());
                  } else if (dataSnapshot.hasError) {
                    return Center(child: Text('Error loading data'));
                  } else {
                    dataSnapshot.data!.docs.forEach((result) {
                      InvoicePayment billPayment =
                          InvoicePayment.fromDocument(result);
                     billPayments.add(billPayment);
                    });

                    return ListView.builder(
                        itemCount: dataSnapshot.data!.size,
                        itemBuilder: (context, index) {
                          return getPaymentLine(index, context);
                        });
                  }
               })));
  }}
Adriaan
  • 17,741
  • 7
  • 42
  • 75
Chatti
  • 1
  • Please take the [tour] and read [ask]. Your questions about code, i.e. debugging questions like these, are served by not only providing the code, but also a textual description on what the code is supposed to do, and what it currently does, see [mre]. Just "It doesn't work" doesn't tell us much. Are the results incorrect? Do you get an error? Does you PC explode in a thousand pieces? Also showing what you have already tried to solve the problem is appreciated. [edit] the question accordingly. – Adriaan Aug 15 '23 at 08:19

0 Answers0