return the total of a field in each document of CompletedTasks collection of a user (flutterflow custom action)

27 views Asked by At

I have a collection 'CompletedTasks' that stores a date, cp_tasks_int (number of items in the list), and a list containing reference to another collection. I want to add the cp_tasks_int in each document in the 'CompletedTasks' collection of the logged in user. This is the code but its giving errors

Error: A value of type 'num' can't be assigned to a variable of type 'int'. Try changing the type of the variable, or casting the right-hand type to 'int'.

Code: import 'dart:convert'; import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago;
import '/flutter_flow/lat_lng.dart';
import '/flutter_flow/place.dart';
import '/flutter_flow/uploaded_file.dart';
import '/flutter_flow/custom_functions.dart';
import '/backend/backend.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import '/backend/schema/structs/index.dart';
import '/auth/firebase_auth/auth_util.dart';

int newCustomFunction() {
   /// MODIFY CODE ONLY BELOW THIS LINE

  // return the total of cp_tasks_int from all documents of CompletedTasks colleciton of user
  int totalCpTasksInt = 0;
   currentUserReference
      ?.collection('CompletedTasks')
      .get()
      .then((querySnapshot) {
    querySnapshot.docs.forEach((doc) {
      totalCpTasksInt += doc.get('cp_tasks_int');
    });
  });
  return totalCpTasksInt;

  /// MODIFY CODE ONLY ABOVE THIS LINE
}
0

There are 0 answers