Search code examples
androidandroid-studioflutterdartcamera

Why doesn't a camera open with flutter?


I am trying to make a camera open using flutter and this is what I get:

E/flutter (26189): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: MissingPluginException(No implementation found for method availableCameras on channel plugins.flutter.io/camera)

E/flutter (26189): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:157:7)

E/flutter (26189): < asynchronous suspension>

E/flutter (26189): #1 availableCameras (package:camera/camera.dart:56:9)

E/flutter (26189): < asynchronous suspension>

E/flutter (26189): #2 main (package:flutter_realtime_detection/main.dart:11:15)

E/flutter (26189): < asynchronous suspension>

this is the line flutter does not accept:

Future<Null> main() async {
   WidgetsFlutterBinding.ensureInitialized();
   try {
      cameras = await availableCameras();
   } on CameraException catch (e) {
      print('Error: $e.code\nError Message: $e.message');
   }
   runApp(new MyApp());
}

I have tried flutter clean, hot reloads but nothing helps(

Here is flutter doctor -v:

[✓] Flutter (Channel master, 1.24.0-8.0.pre.342, on macOS 11.1 20C69 darwin-x64, locale en-GB)
    • Flutter version 1.24.0-8.0.pre.342 at /Users/fimarubin/Developer/flutter \
    • Framework revision 226532b157 (4 weeks ago), 2020-11-21 12:08:04 +0100\
    • Engine revision 23a8e027db\
    • Dart version 2.12.0 (build 2.12.0-62.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)\
    • Android SDK at /Users/fimarubin/Library/Android/sdk\
    • Platform android-30, build-tools 30.0.2\
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java\
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)\
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 12.3)\
    • Xcode at /Applications/Xcode.app/Contents/Developer\
    • Xcode 12.3, Build version 12C33\
    • CocoaPods version 1.10.0

[✓] Android Studio (version 4.1)\
    • Android Studio at /Applications/Android Studio.app/Contents\
    • 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 1.8.0_242-release-1644-b3-6915495)

[✓] Connected device (1 available)\
    • SM G9750 (mobile) • R28M31Y893Y • android-arm64 • Android 10 (API 29)

• No issues found!

Solution

  • Please ensure that the following dependencies are set:

    camera:

    path_provider:

    path:

    dependencies:
      flutter:
        sdk: flutter
      camera:
      path_provider:
      path:
    

    Ensure that plugin services are initialized so that `availableCameras() can be called

    WidgetsFlutterBinding.ensureInitialized();
    
    final cameras = await availableCameras();