12

Whenever I try to run flutter app, I am getting this error.

../flutter/.pub-cache/hosted/pub.dartlang.org/percent_indicator-4.2.1/lib/linear_percent_indicator.dart:162:5: Warning: The class 'WidgetsBinding' cannot be null.
Try replacing '?.' with '.'
    WidgetsBinding?.instance.addPostFrameCallback((_) {
    ^^^^^^^^^^^^^^
../flutter/.pub-cache/hosted/pub.dartlang.org/percent_indicator-4.2.1/lib/linear_percent_indicator.dart:162:30: Error: Method 'addPostFrameCallback' cannot be called on 'WidgetsBinding?' because it is potentially null.
 - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../flutter/packages/flutter/lib/src/widgets/binding.dart').
Try calling using ?. instead.
    WidgetsBinding?.instance.addPostFrameCallback((_) {
                             ^^^^^^^^^^^^^^^^^^^^


FAILURE: Build failed with an exception.

* Where:
Script '/home/user/Downloads/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 1102

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command '/home/user/Downloads/flutter/bin/flutter'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 22s
Running Gradle task 'assembleDebug'...                             22.8s
Exception: Gradle task assembleDebug failed with exit code 1

Here, is my Flutter doctor output

[✓] Flutter (Channel stable, 2.10.3, on Ubuntu 20.04.4 LTS 5.13.0-41-generic, locale en_IN)
    • Flutter version 2.10.3 at /home/user/Downloads/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 7e9793dee1 (3 months ago), 2022-03-02 11:23:12 -0600
    • Engine revision bd539267b4
    • Dart version 2.16.1
    • DevTools version 2.9.2

[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /home/user/Android/Sdk
    • Platform android-32, build-tools 32.1.0-rc1
    • Java binary at: /opt/android-studio-2021.1.1/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Android Studio (version 2021.1)
    • Android Studio at /opt/android-studio-2021.1.1/android-studio
    • Flutter plugin version 66.0.1
    • Dart plugin version 211.7811
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)

[✓] Android Studio
    • Android Studio at /opt/android-studio
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)

[✓] Connected device (2 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 12 (API 31) (emulator)
    • Chrome (web)                 • chrome        • web-javascript • Google Chrome 99.0.4844.82

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

I have tried following things already

  1. Invalidate and restart android-studio
  2. Deleted .gradle folder and try building again
  3. flutter clean and then 'flutter pub get, flutter pub cache repair`
    • also deleted build folder and pubspec.lock file

But nothing, worked

Also, here is my Gradle files

android/app/build.gradle

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.google.gms.google-services'
android {
    compileSdkVersion flutter.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
    splits {
        abi {
            enable true
            reset()
            include 'x86', 'armeabi-v7a'
            universalApk true
        }
    }
    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.app.example"
        minSdkVersion 19
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled = true
    }
    // signingConfigs {
    //     debug {
    //         if (debugKeystorePropertiesFile.exists()) {
    //             keyAlias debugKeystoreProperties['keyAlias']
    //             keyPassword debugKeystoreProperties['keyPassword']
    //             storeFile file(debugKeystoreProperties['storeFile'])
    //             storePassword debugKeystoreProperties['storePassword']
    //         }
    //     }
    // }
    buildTypes {
        release {
//            if (keystorePropertiesFile.exists()) {
//                signingConfig signingConfigs.release
//                println "Signing with key.properties"
//            } else {
                signingConfig signingConfigs.debug
                println "Signing with debug keys"
//            }
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:29.2.1')
    implementation 'com.google.firebase:firebase-auth-ktx'
    implementation 'com.google.android.gms:play-services-safetynet:18.0.1'
    implementation 'com.google.firebase:firebase-appcheck-safetynet:16.0.0-beta05'

    implementation "androidx.browser:browser:1.3.0"
}

android/build.gradle

buildscript {
    ext.kotlin_version = '1.6.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.10'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

  • I do have added android.useAndroidX and android.enableJetifier to true
  • Also added android:exported="true" in androidmanifest.xml
1
  • This issue is caused by percent_indicator: ^4.0.0 I comment the usage of this lib and also removed from pubspec and it's working then. Commented May 16, 2022 at 8:49

10 Answers 10

8

As @DiyorbekDev said, you should use the 4.0.1 version. 4.2.x is meant to Flutter 3 and have some null safety incompatibility issues.

You DO NOT need to add percent_indicator lib to local project. I've got through the same issue. I had to force pubspec.yaml to use the 4.0.1 version:

percent_indicator: 4.0.1

OBS: do not use ^4.0.1 (for some reason, it was pulling the last version [4.2.1] for me)

Sign up to request clarification or add additional context in comments.

3 Comments

okay, Thanks for sharing. I was not aware of that 4.0.1 will work. I will try, let you know.
same here 4.0.1 pulling 4.2.1 so i changed it percent_indicator: git: url: https://github.com/diegoveloper/flutter_percent_indicator ref: 686feb842d7144c3c45f8ce8d305a380416f3b67
Hats off this is exactly what the issue was!
4

Your ? is wrong position. Try to change the code like this

// WidgetsBinding?.instance.addPostFrameCallback
-> WidgetsBinding.instance?.addPostFrameCallback

Comments

3

You should change

WidgetsBinding?.instance.addPostFrameCallback

to

WidgetsBinding.instance?.addPostFrameCallback

Comments

2

seems like the package put the null aware operator on the WidgetsBinding instead of the instance.

dart:162:5: Warning: The class 'WidgetsBinding' cannot be null. Try replacing '?.' with '.' WidgetsBinding?.instance.addPostFrameCallback((_) { ^^^^^^^^^^^^^^

it should be:

WidgetsBinding.instance?.addPostFrameCallback((_) {

Comments

2

Use percent_indicator version 4.0.1. Higher versions are customized to flutter 3.0 so you have an error. Or upgrade the flutter to version 3.0

enter image description here

enter image description here

3 Comments

not in this case. he is using Flutter 2.10.3
Yes i got you and you are right, but look at thread creators flutter doctor output.
1

change your percent_indicator dependency in pubspec.yaml to:

percent_indicator: #update flutter 3
git:
  url: https://github.com/XuannThucc/flutter_percent_indicator.git
  ref: be51b3d56e729ca1889f77a5f6311b5287fa611d

3 Comments

Tried this, but still same error
Do you try run Flutter clean to clear cache and rebuild ?
Yes, after adding percent_indicator git repo to pubspec.yaml, I fired flutter clean and then flutter pub get, also I tried Invalidate/Restart android studio. But didn't worked.
0

change

nb_utils: ^4.4.17

to

nb_utils: 4.4.17

in

pubspec.yaml

Comments

0

Resolved, by adding percent_indicator lib to local project.

In that I changed, this

WidgetsBinding?.instance.addPostFrameCallback((_) {
                             ^^^^^^^^^^^^^^^^^^^^

to this

WidgetsBinding?.instance?.addPostFrameCallback((_) {
                             

It's working

2 Comments

I am having the same issue. What do you mean by "adding percent_indicator lib to local project"?
clone the github repo code and put it inside your project, and do the needful changes, or update your project to latest flutter version or packages
0

I was having this error just now, here is how I fixed:

I had a flutter dependency that I have to downgrade to a very older version, in your case is the percent_indicator-4.2.1.

go to pubspec.yaml

downgrade the percent_indicator-4.2.1 version to an older version

please let me know if it helped.

Comments

0

I faced the same problem, It worked for me by downgrading the package version.

Take a look at this repo: https://github.com/diegoveloper/flutter_percent_indicator/commit/f51e758488ecfbe56bff5c85a22bde3a00536013

Comments

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.