Fix Flutter apps not running at max available frame rate on some devices

Fix Flutter apps not running at max available frame rate on some devices

Alan
Alan
·April 26, 2024·1 min read·
Flutter
/ /
/ /

What's going on?

I've been using a Nothing Phone 2 until one day I realized some apps are not running at 120fps. I thought it was an issue on Flutter, did some searching and found out this Github issue. However the fix requires usage of a package and this is what the package mentioned:

A Flutter plugin to set display mode in Android. This is useful to enable high refresh rate in devices with discrete framerates like 60Hz, 90Hz etc. This library is ineffective on devices with LTPO panels and iOS devices with ProMotion.

The Nothing Phone 2 is using LTPO panel so it doesn't work.

Solution

In MainActivity.kt, override onFlutterSurfaceViewCreated and set window.attributes with preferredRefreshRate of 120F:

MainActivity.ktkotlin
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.android.FlutterSurfaceView

class MainActivity: FlutterActivity() {
    override fun onFlutterSurfaceViewCreated(flutterSurfaceView: FlutterSurfaceView) {
        super.onFlutterSurfaceViewCreated(flutterSurfaceView)
		
        window.attributes = window.attributes.also { it.preferredRefreshRate = 120F }
    }
}

The above solution is only tested on Nothing Phone 2. It might work for other Android devices that have the same issue but you need to test it out yourselves.

Hope it helps :)


/ /
/ /
Alan

Alan

Owner of this site

Loading related posts...