Flutter应用运行失败求助:intent-1.4.0插件Kotlin空接收器编译错误
Hey there, let's get this Flutter build error sorted out together! The issue you're facing comes from null safety violations in the intent plugin's Android Kotlin code. Here's a breakdown of what's going wrong and how to fix it:
What's Causing the Error?
Kotlin enforces strict null safety, which means you can't call methods directly on a nullable object like Intent? without using either a safe call (?.) or a non-null assertion (!!.). The 1.4.0 version of the intent plugin doesn't handle this correctly in four lines of its IntentPlugin.kt file, leading to the compile errors you see.
Solution 1: Upgrade the Intent Plugin (Recommended)
The easiest fix is to use a newer version of the plugin where this null safety issue is likely already resolved:
- Open your project's
pubspec.yamlfile - Locate the
intentdependency line and update it to the latest available version (e.g.,intent: ^2.0.0or higher) - Run
flutter pub getto fetch the updated plugin - Restart your build with
flutter run
Solution 2: Manually Patch the Plugin Code
If upgrading isn't an option for you, you can edit the plugin's source code directly:
- Navigate to the file path from your error message:
D:\src\flutter\.pub-cache\hosted\pub.dartlang.org\intent-1.4.0\android\src\main\kotlin\io\github\itzmeanjan\intent\IntentPlugin.kt - Open the file in a text editor or Android Studio
- Modify the four problematic lines to use safe calls (safer) or non-null assertions (only if you're certain
intentis never null here):- Line 50: Replace
intent.setAction(action)withintent?.setAction(action)orintent!!.setAction(action) - Line 55: Replace
intent.addFlags(flags)withintent?.addFlags(flags)orintent!!.addFlags(flags) - Line 56: Replace
intent.putExtra(EXTRA_STREAM, uri)withintent?.putExtra(EXTRA_STREAM, uri)orintent!!.putExtra(EXTRA_STREAM, uri) - Line 58: Replace
intent.putExtra(key, value)withintent?.putExtra(key, value)orintent!!.putExtra(key, value)
- Line 50: Replace
- Save the file, then run
flutter cleanfollowed byflutter pub getto refresh dependencies - Try launching your app again with
flutter run
Quick Note on Null Safety Syntax
?.is a safe call: it only executes the method if the object isn't null, avoiding crashes!!.is a non-null assertion: it tells Kotlin you guarantee the object isn't null, but will cause a crash if it ever is null at runtime
内容的提问来源于stack exchange,提问作者Syed Saif

