Flutter custom function works on the Test mode of Flutterflow - but not working on mobile app builds
Tags: flutter, dart, ios, android
Published: 2023-07-11 14:52:30

I have this custom function in Dart running on Flutterflow (a platform using Flutter building apps on iOS and Android)

```
import '/backend/backend.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; 
import '/flutter_flow/custom_functions.dart'; 
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

Future<void> updateBalanceAndPointPremium(String documentId) async {
  try {
    final DocumentReference userDocRef =
        FirebaseFirestore.instance.collection('users').doc(documentId);

    final DocumentSnapshot snapshot = await userDocRef.get();
    if (snapshot.exists) {
      final data = snapshot.data() as Map<String, dynamic>;
      final double point = data['Point'] ?? 0.0;
      final double balance = data['Balance'] ?? 0.0;
      final int balanceTimes = data['balanceConvertTimes'] ?? 0;

      double updatedBalance = balance;
      int updatedBalanceTimes = balanceTimes;

      // Calculate the number of times the balance should be increased by 15
      int increaseCount = ((point ~/ 50) - balanceTimes);

      if (increaseCount > 0) {
        updatedBalance += increaseCount * 15;
        updatedBalanceTimes += increaseCount;
      }

      final updateData = {
        'Balance': updatedBalance,
        'balanceConvertTimes': updatedBalanceTimes
      };

      await userDocRef.update(updateData);
      print('Balance field updated successfully!');
    }
  } catch (e) {
    print('Error updating balance field: $e');
  }
}
```

he purpose is to add to the Balance data of the user who has the input documentID on Firebase by 15 when their Point data reaches the thresholds 50, 100, 150, ... The code works well in Test mode (I am using Flutterflow - an application building apps using Dart), but when I submit the app through Codemagic to the App Store and Google Play store, it is not working (Balance data was automatically added 15 as expected) on builds running on Testflight and approved App store app. So I really don't know why this is happening. Please help. Thanks a lot.

I tried to put this function inside another function which works well on both Test mode and real builds. The combined function in the new build still didn't work (even though it works in the Test mode still). The new code can be seen below:

```
import '/backend/backend.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

Future<void> addPointandBalancePremium(
    String documentId, double amountToAdd) async {
  try {
    final DocumentReference userDocRef =
        FirebaseFirestore.instance.collection('users').doc(documentId);

    await FirebaseFirestore.instance.runTransaction((transaction) async {
      final DocumentSnapshot snapshot = await transaction.get(userDocRef);
      if (snapshot.exists) {
        final data = snapshot.data() as Map<String, dynamic>;
        final currentAmount = data['Point'] ?? 0.0;

        final double balance = data['Balance'] ?? 0.0;
        final int balanceTimes = data['balanceConvertTimes'] ?? 0;

        double updatedBalance = balance;
        int updatedBalanceTimes = balanceTimes;

        var currentDist = data['pointToNextReward'] ?? 0.0;
        var currentDistFree = data['pointToNextRewardFree'] ?? 0.0;
        final newAmount = currentAmount + (0.2 * amountToAdd / 100);
        var newDist = 50 - (0.2 * amountToAdd / 100 - currentDist);
        var newDistFree = 100 - (0.2 * amountToAdd / 100 - currentDistFree);

        int increaseCount = ((newAmount ~/ 50) - balanceTimes);

        if (increaseCount > 0) {
          updatedBalance += increaseCount * 15;
          updatedBalanceTimes += increaseCount;
        }

        currentDist = newDist;
        if (newDist >= 50) {
          currentDist = newDist - 50;
        }
        ;

        currentDistFree = newDistFree;
        if (newDistFree >= 100) {
          currentDistFree = newDistFree - 100;
        }
        ;

        transaction.update(userDocRef, {
          'Point': newAmount,
          'pointToNextReward': currentDist,
          'pointToNextRewardFree': currentDistFree,
          'Balance': updatedBalance,
          'balanceConvertTimes': updatedBalanceTimes
        });
      }
    });

    print('Point and Balance fields updated successfully!');
  } catch (e) {
    print('Error updating Point field: $e');
  }
}
```

Everything works in Test mode on Flutterflow but not on real builds. This makes me really confused and stuck.
Please can someone help me with that?