Fixing `DVTPortalServiceErrorDomain: Your session has expired` in Flutter for iOS

These days my iOS app updates for Flutter keep randomly failing with the following error:

DVTPortal: Error: Error Domain=DVTPortalServiceErrorDomain Code=1100 "Your session has expired.  Please log in."

It used to be about once every 5 builds, but then it became 100% of them failing with that error. So I searched for information and ended up on this post. Even though in my account, there was still one non-expired certificate, I added an Apple Development certificate like they suggested, then ran flutter build ipa:

Building and deploying iOS build

Building without sound null safety ⚠️
Dart 3 will only support sound null safety, see

Archiving com.wafrat.flashcards...
Automatically signing iOS for device deployment using specified development team in Xcode project: [redacted]
Running Xcode build...                                                  
Xcode archive done.                                         10.6s
Failed to build iOS app
Error (Xcode): Provisioning profile "iOS Team Provisioning Profile: com.wafrat.flashcards" doesn't include signing certificate "Apple Development: [redacted]".

It appears that there was a problem signing your application prior to installation on the device.

Verify that the Bundle Identifier in your project is your signing id in Xcode
  open ios/Runner.xcworkspace

Also try selecting 'Product > Build' to fix the problem.
Encountered error while archiving for device.

This time, the build failed with another error. They want me to build the app from XCode. So I did that.

Product > Build ==> Build Succeeded!

When the build succeeded, I built the app with the flutter build ipa command again. It kept asking me for the login password. No matter whether I clicked "Always allow" or "Allow", it would pop up that exact same prompt. After typing it 10 times, I pressed Escape to abort. I had to press it 10 times as well to close them all. When the last password prompt dialog disappeared, it started compiling. Strange behavior.

This prompt kept popping up.

It compiled then finally uploaded it it to the App Store Connect successfully:

Successfully uploaded the new binary to App Store Connect

However, a day or two after pushing successful updates, it failed once again. Looks like it's still sporadic and I may not have fixed it entirely yet. We'll see what happens in the next few days.