Home > Software engineering >  Calling availableCameras() occurs MissingPluginException
Calling availableCameras() occurs MissingPluginException

Time:11-03

We are testing calling Native code from a simple Flutter app with a camera.

However, an error occurs at the point where we call availableCameras(), which has nothing to do with our native integration code.

MissingPluginException (MissingPluginException(No implementation found for method availableCameras on channel plugins.flutter.io/camera_android))
DartPluginRegistrant.ensureInitialized();
WidgetsFlutterBinding.ensureInitialized();
var cameras = await availableCameras();

We lowered the version of camera but it did not improve. Of course we did flutter clean and flutter pub get each time, and also restarted Visual Studio Code and Android Studio.

pubspec.yaml

dependencies:
  flutter:
    sdk: flutter

  camera: ^0.10.0 4
  # camera: 0.9.7 1
  path_provider: ^2.0.11
  path: ^1.8.2

# dependency_overrides:
  # camera_android: 0.9.7 1
  # camera_avfoundation: 0.9.7 1

We were testing it to call Native code from Flutter. This problem did not occur before we tried it. So we think it is a possible cause. We suspected a duplicate channel name and changed it to include our domain name, but this did not improve things.

class AppInfo {
  static const MethodChannel _channel = MethodChannel('appInfo');

  static Future<String?> get appVersion async {
    final String? version = await _channel.invokeMethod('getAppVersion');
    return version;
  }
}

MainActivity.kt

package com.xxx.xxxx

import android.content.pm.PackageManager
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import kotlinx.coroutines.ExperimentalCoroutinesApi

class MainActivity : FlutterActivity() {
    private val CHANNEL = "appInfo"

    @OptIn(ExperimentalCoroutinesApi::class)
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        MethodChannel(
            flutterEngine.dartExecutor.binaryMessenger,
            CHANNEL
        ).setMethodCallHandler { call, result ->
            if (call.method == "getAppVersion") {
                try {
                    val pInfo =
                        applicationContext.packageManager.getPackageInfo(context.packageName, 0)
                    var version = pInfo.versionName
                    result.success("${pInfo.versionName}");

                } catch (e: PackageManager.NameNotFoundException) {
                    e.printStackTrace()
                    result.notImplemented()
                }
            } else {
                result.notImplemented()
            }
        }
    }
}

The iOS version does not have this problem.

AppDelegate.swift

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)

      let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
      let helloMethodChannel = FlutterMethodChannel(name: "appInfo",binaryMessenger: controller as! FlutterBinaryMessenger)
      helloMethodChannel.setMethodCallHandler({
        (call:FlutterMethodCall,result:FlutterResult) -> Void in

        switch call.method{
            case "getAppVersion":
                result(Bundle.main.object(forInfoDictionaryKey:"CFBundleShortVersionString") as? String)
            default:
               result("iOS"   UIDevice.current.systemVersion)
        }

      })

    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

What else can we try?

flutter doctor -v

[✓] Flutter (Channel stable, 3.3.6, on macOS 12.6 21G115 darwin-x64, locale en-JP)
    • Flutter version 3.3.6 on channel stable at /Users/ganessa/fvm/versions/stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 6928314d50 (7 days ago), 2022-10-25 16:34:41 -0400
    • Engine revision 3ad69d7be3
    • Dart version 2.18.2
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/ganessa/src/android-sdks
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12 0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 13F100
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
                 
  • Related