how to get Firebase sum of txn amount i got error The operator can t be unconditionally invoked because the receiver can be null

0 votes

The todaytxn() function performs the following steps:

It gets the current date and time using the DateTime.now() function and creates a new DateTime object with only the year, month, and day parts.

It converts the DateTime object to a string in the format "yyyy-MM-dd" using the toString() function and the substring() method to extract the first 10 characters.

It queries the payment collection in Firestore using the where() method to filter the documents based on the txnDate field. It sets the minimum and maximum values of the txnDate field to be the start and end of the current day, respectively.

It retrieves the query results using the get() method and then uses the then() method to handle the asynchronous response.

It retrieves the total number of transactions by getting the length of the docs list in the QuerySnapshot object.

It retrieves the total transaction amount by using the fold() method to iterate over the docs list and accumulate the txnAmount field values. The fold() method takes an initial value of 0 and a callback function that takes two arguments: the accumulated value (starting with the initial value) and the current document being processed. The callback function returns the sum of the accumulated value and the current document's txnAmount field value. The toInt() method is used to convert the accumulated value to an integer.

It prints the total number of transactions and the total transaction amount to the console using the print() function. Code i used below

todaytxn() async {
  DateTime now = DateTime.now();
  DateTime today = DateTime(now.year, now.month, now.day);
  print(today);

  String todayString = today.toString().substring(0, 10); // convert DateTime to string in format "yyyy-MM-dd"
  print(todayString);

  FirebaseFirestore.instance.collection('payment')
      .where('txnDate', isGreaterThanOrEqualTo: todayString)
      .where('txnDate', isLessThan: todayString + 'T23:59:59') // add time part to create range
      .get()
      .then((QuerySnapshot querySnapshot) {
    int numTransactions = querySnapshot.docs.length;
    print('Total transactions for today: $numTransactions');
  });

  FirebaseFirestore.instance.collection('payment')
      .where('txnDate', isGreaterThanOrEqualTo: todayString)
      .where('txnDate', isLessThan: todayString + 'T23:59:59') // add time part to create range
      .get()
      .then((QuerySnapshot querySnapshot) {
    int numTransactions = querySnapshot.docs.length;
    print('Total transactions for today: $numTransactions');

    int totalTxnAmt = querySnapshot.docs.fold(0, (acc, doc) => acc + (doc['txnAmount'] ?? 0)).toInt();
        
    print('Total txnAmt for today: $totalTxnAmt');
  });
}

Apr 10, 2023 in Flutter by Ashwini
• 5,430 points
1,359 views

1 answer to this question.

0 votes
The error message "The operator can't be unconditionally invoked because the receiver can be null" indicates that the code is trying to access a property or method of a nullable object without checking if it is null or not. In your code, the error is likely caused by the expression doc['txnAmount'] ?? 0 in the fold() method callback function.

To fix this error, you can add a null check to ensure that the doc['txnAmount'] property is not null before using it. You can do this by replacing the doc['txnAmount'] ?? 0 expression with (doc['txnAmount'] != null) ? doc['txnAmount'] : 0. This expression checks if doc['txnAmount'] is not null and returns its value if it is not null, otherwise it returns 0.
answered Apr 10, 2023 by pooja

Related Questions In Flutter

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How can I change the app display name build with Flutter?

Yes, you can change the app display ...READ MORE

answered Mar 21, 2023 in Flutter by venky
2,333 views
0 votes
1 answer

How to find the path of Flutter SDK?

To locate the Flutter SDK, you can ...READ MORE

answered Mar 27, 2023 in Flutter by anonymous
26,224 views
0 votes
1 answer

flutter firebase: showing data

It seems that when using different emulators ...READ MORE

answered Apr 4, 2023 in Flutter by vishalini
616 views
0 votes
1 answer

Cloud Firestore exception handling

Cloud Firestore throws a variety of exceptions ...READ MORE

answered Apr 6, 2023 in Flutter by veena
1,700 views
0 votes
1 answer

flutter app not running on ios simulator after adding firebase notification

The error message you provided seems to ...READ MORE

answered Apr 10, 2023 in Flutter by vishalini
4,886 views
0 votes
2 answers

Google Cloud datastore vs firebase

Google has already made a guide for ...READ MORE

answered Aug 20, 2018 in GCP by Priyaj
• 58,090 points
1,613 views
0 votes
1 answer
0 votes
1 answer

How to Deserialize a list of objects from json in flutter Ask Question?

To deserialize a list of objects from ...READ MORE

answered Mar 28, 2023 in Flutter by vishalini
1,943 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP