0

I know there are a lot of questions about this exception but no answer suited my case.

var count = sharedPref.getInt("flutter.badgeCount", 0)    // line 12
ShortcutBadger.applyCount(applicationContext, count+1)    // line 13

count should be an integer because sharedPref.getInt returns an integer, and applyCount() receive an integer as the second parameter. The exception is thrown at runtime at line 12. Is there anything I can't see? (I'm pretty new to kotlin)

2
  • 1
    You must have a long value stored in the shared preferences for that key. Commented Dec 9, 2020 at 18:29
  • "because sharedPref.getInt returns an integer" if it succeeds. In your case it fails, so the stored value is not an integer (and from the error message we know it's a long). Line 13 doesn't matter because you never get to there. Commented Dec 10, 2020 at 13:24

1 Answer 1

2

Use the following. This would solve the problem.

var count = sharedPref.getLong("flutter.badgeCount", 0L)  
ShortcutBadger.applyCount(applicationContext, count.toInt()+1)
Sign up to request clarification or add additional context in comments.

2 Comments

I got an error: "Type mismatch: inferred type is Long but Int was expected".
Ok, I cast count to integer with count.toInt() and now works fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.