قم بحل مشكل بطئ الكود 
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:encrypt/encrypt.dart' as encrypt;
import 'dart:math';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'توليد مستخدمين',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: const UserGeneratorScreen(),
    );
  }
}

class UserGeneratorScreen extends StatefulWidget {
  const UserGeneratorScreen({super.key});

  @override
  _UserGeneratorScreenState createState() => _UserGeneratorScreenState();
}

class _UserGeneratorScreenState extends State<UserGeneratorScreen> {
  final String _apiUrl = "api/MicroSerial";
  final String _encryptionKey = "my32lengthsupersecretnooneknows1";
  final String _iv = "8bytesiv12345678";

  bool _isLoading = false;
  String _resultMessage = '';
  bool _isSuccess = false;
  String? _urlApi;

  final String firebaseUrl =
      'https://micropanel-b67e0-default-rtdb.firebaseio.com/information.json';

  @override
  void initState() {
    super.initState();
    _fetchAndDecryptUrlApi();
  }

  Future<void> _fetchAndDecryptUrlApi() async {
    try {
      final response = await http.get(Uri.parse(firebaseUrl));
      if (response.statusCode == 200) {
        final data = jsonDecode(response.body);
        if (data != null && data['Urlapi'] != null) {
          _urlApi = _decryptAES(data['Urlapi'], _encryptionKey, _iv);
          setState(() {});
        }
      }
    } catch (_) {}
  }

  String _decryptAES(String encryptedText, String keyString, String ivString) {
    final key = encrypt.Key.fromUtf8(keyString);
    final iv = encrypt.IV.fromUtf8(ivString);
    final encrypter = encrypt.Encrypter(
      encrypt.AES(key, mode: encrypt.AESMode.cbc),
    );
    final cleaned = encryptedText.replaceAll(RegExp(r'\s+'), '');
    final encrypted = encrypt.Encrypted.fromBase64(cleaned);
    return encrypter.decrypt(encrypted, iv: iv);
  }

  // دالة لتوليد سلسلة مكونة من أحرف كبيرة وأرقام فقط، بالحجم المحدد
  String _generateSerialPart(int length) {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    final rand = Random.secure();
    return String.fromCharCodes(
      Iterable.generate(
        length,
        (_) => chars.codeUnitAt(rand.nextInt(chars.length)),
      ),
    );
  }

  String _encryptAES(String plainText, String keyString, String ivString) {
    final key = encrypt.Key.fromUtf8(keyString);
    final iv = encrypt.IV.fromUtf8(ivString);
    final encrypter = encrypt.Encrypter(
      encrypt.AES(key, mode: encrypt.AESMode.cbc),
    );
    final encrypted = encrypter.encrypt(plainText, iv: iv);
    return encrypted.base64
        .replaceAll('+', '-')
        .replaceAll('/', '_')
        .replaceAll('=', '');
  }

  Future<void> _generateAndAddUser() async {
    if (_urlApi == null) return;
    setState(() {
      _isLoading = true;
      _resultMessage = '';
    });

    try {
      final randomId = Random().nextInt(1000000).toString();

      // توليد serialPart1 و serialPart2 كل منهما 6 خانات، ويجب أن يكونا مختلفين
      String serialPart1 = _generateSerialPart(6);
      String serialPart2;
      // ضمان اختلاف 'serialPart2' عن 'serialPart1'
      do {
        serialPart2 = _generateSerialPart(6);
      } while (serialPart2 == serialPart1);

      final encryptedSerial1 = _encryptAES(serialPart1, _encryptionKey, _iv);
      final encryptedSerial2 = _encryptAES(serialPart2, _encryptionKey, _iv);

      final userData = {
        "id": randomId,
        "serialPart1": encryptedSerial1,
        "serialPart2": encryptedSerial2,
      };

      final response = await http.post(
        Uri.parse('$_urlApi/$_apiUrl'),
        headers: {'accept': '*/*', 'Content-Type': 'application/json'},
        body: jsonEncode(userData),
      );

      setState(() {
        if (response.statusCode == 201) {
          _isSuccess = true;
          _resultMessage =
              'تم إضافة المستخدم بنجاح!\nID: $randomId\nserialPart1: $serialPart1\nserialPart2: $serialPart2';
        } else {
          _isSuccess = false;
          _resultMessage =
              'فشل في إضافة المستخدم. رمز الخطأ: ${response.statusCode}\nالرد: ${response.body}';
        }
      });
    } catch (e) {
      setState(() {
        _isSuccess = false;
        _resultMessage = 'حدث خطأ: $e';
      });
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('توليد مستخدمين وإضافتهم إلى API')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed:
                    (!_isLoading && _urlApi != null)
                        ? _generateAndAddUser
                        : null,
                child:
                    _isLoading
                        ? const SizedBox(
                          width: 24,
                          height: 24,
                          child: CircularProgressIndicator(strokeWidth: 3),
                        )
                        : const Text('توليد مستخدم وإضافته'),
              ),
              const SizedBox(height: 20),
              if (_resultMessage.isNotEmpty)
                Container(
                  padding: const EdgeInsets.all(16),
                  decoration: BoxDecoration(
                    color: _isSuccess ? Colors.green[100] : Colors.red[100],
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Text(
                    _resultMessage,
                    style: TextStyle(
                      color: _isSuccess ? Colors.green[800] : Colors.red[800],
                    ),
                    textAlign: TextAlign.center,
                  ),
                ),
            ],
          ),
        ),
      ),
    );
  }
}