0

Trying to generate a QrImage that uses a string generated using 'dart-otp'.

Here is my code.

FutureBuilder(
  future: getUserAttribute(),
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      String secret = snapshot.data.toString();
      final qrData = OTP.generateTOTPCodeString(
        secret,
        DateTime.now().millisecondsSinceEpoch,
        interval: 30,
      );
      safePrint(qrData);
      safePrint(OTP.remainingSeconds());
      return QrImage(
        data: qrData,
        size: 300.0,
      );
    } else {
      return const CircularProgressIndicator();
    }
  },
),

getUserAttribute() is just an async function that returns the current user's email address.

Although the OTP/TOTP generator interval should default to 30 seconds, according to docs, I've explicitly passed the same as a parameter as this is where I am having my problem. Specifically, when I check the second safePrint() on the console, it never starts at 30 seconds. It starts anywhere between 1~29 seconds and only after it counts down to 0 for the first time, it correctly resets to 30 seconds.

I've played around with the DateTime.now(), but did not solve.

What am I doing wrong here?

kenta_desu
  • 303
  • 1
  • 9

1 Answers1

0

Ok, so turns out it was just a misunderstanding on my part.

My original assumption was that the OTP interval starts at 30 seconds from when the code is generated locally. However, this did not turn out to be the case. *My english will start to get fuzzy here, but the interval doesn't start locally, it runs on a global setting.

If my understanding is wrong, please let me know, leaving question up just for record and my own studies.

kenta_desu
  • 303
  • 1
  • 9