[Solved]-"platformexception (platformexception(null-error, host platform returned null value for non-null return value., null, null))"


Flutter exception-: "Unhandled Exception: PlatformException(null-error, Host platform returned null value for non-null return value., null, null)"
I solved this issue by downgrading 'com.google.gms:google-services' from version 4.4.0 to 4.3.13, upgrading 'com.android.tools.build:gradle' from version 7.2.2 to 8.0.2, and keeping 'ext.kotlin_version' as '1.9.0'.

Solution for PlatformException in Flutter

The error message "Unhandled Exception: PlatformException(null-error, Host platform returned null value for non-null return value., null, null)" in Flutter typically indicates an issue where the Flutter application expected a non-null value from the host platform but received a null value instead.

Possible Causes:

  • Null value being returned unexpectedly from a method or operation in the platform-specific code (Android/iOS).
  • Issues with platform-specific configurations or dependencies.
  • Incompatibility between Flutter version and platform-specific code.

Solution:

  1. Check Platform-Specific Code: Review the platform-specific code (Java/Kotlin for Android, Objective-C/Swift for iOS) that interacts with the Flutter application. Ensure that all methods return non-null values as expected.
  2. Debugging: Use debugging techniques to identify the specific location in the platform-specific code where the null value is being returned. Inspect variables and method calls to pinpoint the issue.
  3. Update Dependencies: Make sure that all platform-specific dependencies used in your Flutter project are up-to-date. Update any outdated dependencies to their latest versions to avoid compatibility issues.
  4. Null Safety: If you're using null safety in your Flutter project, ensure that all platform-specific code is also null safe. Use null safety features such as null-aware operators (?. and ??) to handle potential null values.
  5. Testing: Write comprehensive tests for platform-specific code to catch any potential null value issues before they occur in production.
  6. Flutter Doctor: Run the Flutter doctor command to check for any platform-specific setup issues or missing dependencies. Resolve any issues reported by Flutter doctor to ensure a smooth development experience.

After applying the above steps and fixing the null value issue in the platform-specific code, rebuild your Flutter application and test it again. Ensure that the error no longer occurs during runtime.

If the previous solution didn't work for you, you can try this alternative. The problem may lie in Flutter's inability to connect to Firebase, which could be caused by an incorrectly generated google-services.json file or pasting it in the wrong location. Firebase provides a Flutter tool to automatically inject the required configurations, which worked for me. You can find more information about it here: Firebase Flutter Overview This tool automatically fetches the configuration and uploads it to Firebase. Prerequisites: Install the Firebase CLI using the following command: curl -sL https://firebase.tools | bash
I fixed that issue by changing Firebase initialization in main.dart as follows: await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);