Flutterfire: [firebase_database] java.lang.Integer cannot be cast to java.lang.Long on calling FirebaseDatabase.setPersistenceCacheSizeBytes on android

Created on 2 Sep 2019  路  14Comments  路  Source: FirebaseExtended/flutterfire

in firebase_database v3.0.7 on calling FirebaseDatabase.instance.setPersistenceCacheSizeBytes(10000000); I get the following error on Android

E/MethodChannel#plugins.flutter.io/firebase_database(30947): Failed to handle method call
E/MethodChannel#plugins.flutter.io/firebase_database(30947): java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
E/MethodChannel#plugins.flutter.io/firebase_database(30947):    at io.flutter.plugins.firebase.database.FirebaseDatabasePlugin.onMethodCall(FirebaseDatabasePlugin.java:284)
E/MethodChannel#plugins.flutter.io/firebase_database(30947):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
E/MethodChannel#plugins.flutter.io/firebase_database(30947):    at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
E/MethodChannel#plugins.flutter.io/firebase_database(30947):    at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:643)
E/MethodChannel#plugins.flutter.io/firebase_database(30947):    at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_database(30947):    at android.os.MessageQueue.next(MessageQueue.java:323)
E/MethodChannel#plugins.flutter.io/firebase_database(30947):    at android.os.Looper.loop(Looper.java:135)
E/MethodChannel#plugins.flutter.io/firebase_database(30947):    at android.app.ActivityThread.main(ActivityThread.java:5461)
E/MethodChannel#plugins.flutter.io/firebase_database(30947):    at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_database(30947):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
E/MethodChannel#plugins.flutter.io/firebase_database(30947):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
E/flutter (30947): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, java.lang.Integer cannot be cast to java.lang.Long, null)
E/flutter (30947): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)
E/flutter (30947): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
E/flutter (30947): <asynchronous suspension>
E/flutter (30947): #2      FirebaseDatabase.setPersistenceCacheSizeBytes (package:firebase_database/src/firebase_database.dart:120:40)
E/flutter (30947): <asynchronous suspension>
product crowd android database customer critical bug

Most helpful comment

When this fix will be merged and published? It seems an easy to fix error, and somebody already submitted a pull request..

All 14 comments

I get the same problem and had to revert to 3.0.6 or earlier.

Looks like the relevant change happened here:
https://github.com/FirebaseExtended/flutterfire/commit/df54899f6144b00e81229fca33f439242b858454#diff-75d8ced9bfc6b037b8cbc18da165fcd5L282

The previous code correctly pulled it out as an Integer and then it was widened into a long primitive. The updated code removes the autoboxing / unboxing warning, but tries to take the same Integer object and cast it to a Long. Primitives can widen automatically like that (ie: a long can be assigned an int value), but object cannot.

This looks to be a pretty simple fix. I went ahead and created a PR for it:
https://github.com/FirebaseExtended/flutterfire/pull/85

I have experienced the same issue in my App as well when trying to set cache bytes

E/flutter ( 4571): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, java.lang.Integer cannot be cast to java.lang.Long, null)
E/flutter ( 4571): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter ( 4571): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
E/flutter ( 4571): <asynchronous suspension>
E/flutter ( 4571): #2      FirebaseDatabase.setPersistenceCacheSizeBytes (package:firebase_database/src/firebase_database.dart:120:40)

I have the same issue here.. Reverting to an old version doesn't help either.

Screenshot 2019-10-07 at 12 53 01
Screenshot 2019-10-07 at 12 54 59

@solokingjec Try specifying the exact version

firebase_database: 3.0.6

instead of something like

firebase_database: ^3.0.6

in your pubspec.yaml

Then run flutter pub get again to go back to the older version. Something like ^3.0.4 will not revert back to an earlier version if there is a higher minor version available.

When this fix will be merged and published? It seems an easy to fix error, and somebody already submitted a pull request..

Is there any news on this fix? The version of Firebase Database that I am using is 3.1.0. The weird thing about this is that I am only getting the error about the persistence when running on Android but not on the iPhone.
I am trying to set the persistence to be at 10MB with this:

_firebaseDatabase.setPersistenceEnabled(true); _firebaseDatabase.setPersistenceCacheSizeBytes(10000000);

I have been getting this error:

E/flutter (32212): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, java.lang.Integer cannot be cast to java.lang.Long, null) E/flutter (32212): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7) E/flutter (32212): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33) E/flutter (32212): <asynchronous suspension> E/flutter (32212): #2 FirebaseDatabase.setPersistenceCacheSizeBytes (package:firebase_database/src/firebase_database.dart:120:40)

Same issue. Using the version 3.1.1

Any new on a potential work around?

Created a new PR based on the latest code: https://github.com/FirebaseExtended/flutterfire/pull/1708

In the mean time, you can work around the issue by pointing to the fix hosted in my repo:

  firebase_database:
    git:
      url: https://github.com/jpeiffer/flutterfire
      path: packages/firebase_database
      ref: 2d336caeb9ea65fcaaf83035e1653415f7017ae6

any news?

While waiting @jpeiffer fix being merged I try find workaround by using number bigger than maximumjava.Lang.integer (2,147,483,647) and its not throwing any error

db.setPersistenceCacheSizeBytes(2147483648);

Doing so will set persistent cache to 2GB

Hello friends, any news?

This has been merged - will release later on once access has been sorted.

Fix available in 3.1.5

Was this page helpful?
0 / 5 - 0 ratings