Sufyan Kamil
Sufyan Kamil

Reputation: 41

Flutter UI Freezes Despite Using compute for Decryption Function

I'm facing an issue with a decryption function in my Flutter app. I've moved the decryption logic to a top-level function and used compute to offload the heavy computation to a separate isolate. Despite this, the UI still freezes while the decryption is in progress.

Here's a simplified version of my code:

// Top-level function for decryption logic
Future<String?> _aesCBCDecrypt(Map<String, String> params) async {....}
// Main function to be called
Future<String?> aesCBCDecrypt(
  String encryptedText,
  String encryptionSecretKey,
  String ivHex,
  String saltHex,
) async {
  return await compute(_aesCBCDecrypt, {
    'encryptedText': encryptedText,
    'encryptionSecretKey': encryptionSecretKey,
    'ivHex': ivHex,
    'saltHex': saltHex,
  });
}
class DecryptButton extends StatefulWidget {
  @override
  _DecryptButtonState createState() => _DecryptButtonState();
}

class _DecryptButtonState extends State<DecryptButton> {
  bool isLoading = false;
  String? decryptedText1;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ElevatedButton(
            onPressed: () async {
              setState(() {
                isLoading = true;
              });

              // Define the required variables
              String text = 'abc123xyz';
             
              const ivHex = "ivKey"; 
              const saltKey = "saltKey"; 

              // Perform decryption
              final decryptedData = await aesCBCDecrypt(text, decryptSecret, ivHex, saltKey);

              print('Decrypted Data: $decryptedData');

              setState(() {
                isLoading = false;
                decryptedText1 = decryptedData;
              });

              print('Decrypted Text: $decryptedText1');
            },
            child: isLoading ? CircularProgressIndicator() : const Text('decrypt text'),
          ),
          if (decryptedText1 != null) Text(decryptedText1!),
        ],
      ),
    );
  }
}

Despite using compute to offload the decryption, the UI still freezes.

Is there something I'm missing? How can I ensure the UI remains responsive while performing the decryption?

Any help would be greatly appreciated!

Tried using compute to offload the task but still UI freezes.

Upvotes: 0

Views: 72

Answers (0)

Related Questions