← Back to list

flutter local_auth_darwin error: reference to captured var ‘self’ in concurrently-executing code

Unwrap weak self into an immutable constant

Crown.Hg · 2026-05-15 07:32 · 0 claps · 0.7 min read
#flutter #macos #local-authentication #concurrency
Open on Medium ↗
Wiki topics: 📱 · Mobile Development

flutter local_auth_darwin error: reference to captured var ‘self’ in concurrently-executing code

Issue Summary

  • Error: error: reference to captured var ‘self’ in concurrently-executing code
  • Cause: In local_auth_darwin-2.0.3, a nested closure (DispatchQueue.main.async inside context.evaluatePolicy) references a mutable, captured self. Newer Swift compilers (Xcode 14.2+) enforce strict concurrency checks and flag this as thread-unsafe because self can modify or deallocate concurrently.

Solution

Explicitly capture self as an immutable local constant (strongSelf) before passing it into the nested closure to guarantee thread safety.

Before

context.evaluatePolicy(policy, localizedReason: strings.reason) { [weak self] (success: Bool, error: Error?) in
DispatchQueue.main.async {
self?.handleAuthReply(success: success, error: error, options: options, strings: strings, completion: completion)
}
}

After (Fixed)

context.evaluatePolicy(policy, localizedReason: strings.reason) { [weak self] (success: Bool, error: Error?) in
// 1. Unwrap weak self into an immutable constant
guard let strongSelf = self else { return }
// 2. Explicitly capture the immutable constant in the main queue closure
DispatchQueue.main.async { [strongSelf] in
strongSelf.handleAuthReply(success: success, error: error, options: options, strings: strings, completion: completion)
}
}

Steps to Apply

  1. Open file: ~/.pub-cache/hosted/pub.dev/local_auth_darwin-2.0.3/darwin/local_auth_darwin/Sources/local_auth_darwin/LocalAuthPlugin.swift
  2. Replace the block around line 91 with the fixed code above.
  3. Save the file and run:flutter clean && flutter run.

메타데이터
post_id
90947b4e04da
slug
flutter-local-auth-darwin-error-reference-to-captured-var-self-in-concurrently-executing-code-90947b4e04da
url
https://medium.com/@crown.hg/flutter-local-auth-darwin-error-reference-to-captured-var-self-in-concurrently-executing-code-90947b4e04da
canonical_url
https://medium.com/@crown.hg/flutter-local-auth-darwin-error-reference-to-captured-var-self-in-concurrently-executing-code-90947b4e04da
author_url
https://medium.com/@crown.hg
status
ok
fetched_at
2026-06-18 00:10:23