skip to Main Content

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');
  }
}

The 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.

2

Answers


  1. There might be some reasons that you are unable to run you code in real builds:

    • When testing your app in debug mode, some errors or
      issues may not surface. However, in a release build, errors can cause
      the app to crash or behave unexpectedly. Make sure to test your app
      in release mode to identify any issues that may not be visible in
      debug mode.

    • Flutter Version: Ensure that you are using the same Flutter version
      for both your local development environment (where you tested the
      app) and the production build environment. Sometimes, different
      Flutter versions can introduce breaking changes or compatibility
      issues.

    • Dependency Conflicts: Check your project’s dependencies and ensure
      that they are up to date and compatible with each other. Conflicting
      or outdated dependencies can lead to unexpected behavior in release
      builds. Review the dependencies in your pubspec.yaml file and ensure
      that they are compatible with each other and the Flutter version you
      are using.

    • Optimizations: In some cases, certain optimizations applied during
      the release build process can impact the behavior of your app. For
      example, if you have used flutter_flow code generation extensively,
      ensure that the release build process does not strip away any
      necessary code or resources.

    Please let me know if it works for you.

    Login or Signup to reply.
  2. Add internet access to the app

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search