0
votes

I have a program that will check a password with bcrypt library, this is quite computing intensive, so as a result the UI will be stuck for like 2 seconds. It is very annoying and I cannot figure out what to do to stop it.

I want a loader to be shown when the password is being checked.

This is my code:

class _MyWidgetState<MyWidget> extends State{
  build() {
    return GetPassCode(PassCodeType.ENTER,
                  onDone: ({context, data}) async {
                unlock(state, data?['password'] ?? '', Languages.of(context));
              }, goBack: () {}, data: {});
  }

  unlock(userState, String? password, Languages strings) async {
    final user = userState.currentUser;
    if (!(await user.checkPassword(password))) {
        return;
      }
    }
    context.read<LockCubit>().unlock();
  }
}
1

1 Answers

0
votes

you can put the caculating into a isolate.

https://api.flutter-io.cn/flutter/dart-isolate/dart-isolate-library.html

here's some example code:

class IsoMessage {
  final SendPort? sendPort;
  final List<String> args;

  IsoMessage(this.sendPort, this.args);
}

String myCaculate(IsoMessage message) {
  String result = message.args[0][0] + message.args[1][1];
  message.sendPort?.send(result);
  return result;
}

here's how to calling the func

var port = ReceivePort();
port.listen((message) {
   print("onData: $message");
}, onDone: () {
   print('iso close');
}, onError: (error) {
   print('iso error: $error');
});
IsoMessage message = IsoMessage(port.sendPort,["asd", "dsa"]);
Isolate.spawn<IsoMessage>(myCaculate, message);