Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

flutter_local_notifications crashes on Android with Integer.intValue() on a null object reference

ErrorAttempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference

Short answer

The crash comes from the small icon. A scheduled notification fired with no icon on the notification, no default icon saved at initialisation and no legacy icon id, so Android got a null number. Upgrade flutter_local_notifications to 23.0.0 or newer, and always give the plugin a default icon that exists as a drawable.

  • Applies to Flutter 3.3x, flutter_local_notifications 23.0.0 or newer, Android
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the plugin version you actually resolved, then read the crash from the device log. If the stack trace goes through setSmallIcon, both causes below apply.

    flutter pub deps | grep flutter_local_notifications
    adb logcat -d | grep -A 12 "intValue()"
  2. 2Match what you see, then jump to its fix

    1. 1The crash happens when a scheduled notification is delivered, often after the app was updated; the plugin version is below 23.0.0An old plugin version crashes when the icon data is missingFix
    2. 2No icon is set on the notification, and initialize was never given a default icon; the crash shows when a scheduled notification is deliveredThe plugin was never given a small icon that existsFix

What does the error look like?

  • Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
  • NullPointerException in setSmallIcon

Cause 1 of 2

An old plugin version crashes when the icon data is missing

1Check

The plugin's changelog lists a fix in 23.0.0 for exactly this NullPointerException in setSmallIcon, for a scheduled notification delivered with no icon, no persisted default icon and a null legacy icon resource id. The fix falls back to the application's own icon. Compare your resolved version with that.

flutter pub deps | grep flutter_local_notifications
flutter pub outdated

2Fix

Upgrade to 23.0.0 or newer. Read the changelog entries between your old version and the new one first, because a newer version can ask for newer Android build settings; the README of the version you move to lists them.

flutter pub upgrade --major-versions flutter_local_notifications

3Verify

Schedule a notification a minute ahead, close the app and wait. The notification shows and the log has no new NullPointerException.

adb logcat -d | grep -c "intValue()"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

The plugin was never given a small icon that exists

1Check

The plugin takes its default icon from AndroidInitializationSettings, and the README says that icon has to be added as a drawable resource to the Android project. A drawable resource lives under res/drawable. Check that the name you pass has a file there.

grep -rn "AndroidInitializationSettings" lib/
ls android/app/src/main/res/drawable*

2Fix

Add a small notification icon as a drawable, pass its name when you initialise the plugin, and optionally name it again on each notification through the icon argument of AndroidNotificationDetails.

// default icon: the file res/drawable/app_icon.png must exist
const AndroidInitializationSettings androidSettings =
    AndroidInitializationSettings('app_icon');

// optional, per notification
AndroidNotificationDetails(
  channelId,
  channelName,
  icon: 'app_icon',
)

3Verify

Show a notification right away and then a scheduled one. Both appear with your icon.

flutter run

Confirmed by pub.dev

Did this fix it?

How do I stop it coming back?

  • Keep flutter_local_notifications current and read its changelog when a major version appears.
  • Add the notification icon as a drawable in the first commit that adds notifications, and initialise the plugin with its name.
  • Test the scheduled path as well as show(): schedule one a minute ahead and close the app.
  • Check release builds too: the plugin's README warns that resources such as notification icons can be removed by R8 unless you keep them.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

Flutter Android: Failed to load FirebaseOptions from resource

Failed to load FirebaseOptions from resource. Check that you have defined values.xml correctly.

On Android, Firebase reads its settings from a values.xml file that the Google services Gradle plugin builds from google-services.json.

4 causes, each with a fix

Mobile apps

Flutter's Android build stops: JAVA_HOME is not set

ERROR: JAVA_HOME is set to an invalid directory

The Gradle wrapper script needs a Java runtime and looks in two places: the JAVA_HOME variable, then a java command on PATH.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.