Wazza
Wazza

Reputation: 1885

Flutter camera plugin not importing/useable

I am getting the following error when beginning to implement the camera plugin on my flutter app:

  [VERBOSE-2:dart_error.cc(16)] Unhandled exception:
  MissingPluginException(No implementation found for method init on channel plugins.flutter.io/camera)
  #0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:153:7)
  <asynchronous suspension>
  #1      _channel (package:camera/camera.dart:7:5)
  #2      _channel (package:camera/camera.dart:6:21)
  #3      availableCameras (package:camera/camera.dart:42:41)
  <asynchronous suspension>
  #4      main (file:///Users/waynerumble/Desktop/scott_and_viki/lib/main.dart:10:19)
  <asynchronous suspension>
  #5      _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:279:19)
  #6      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)
  [VERBOSE-2:dart_error.cc(16)] Unhandled exception:
  MissingPluginException(No implementation found for method list on channel plugins.flutter.io/camera)
  #0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:153:7)
  <asynchronous suspension>
  #1      availableCameras (package:camera/camera.dart:42:50)
  <asynchronous suspension>
  #2      main (file:///Users/waynerumble/Desktop/scott_and_viki/lib/main.dart:10:19)
  <asynchronous suspension>
  #3      _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:279:19)
  #4      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)

The error doesn't occur until i start using the plugin itself, i.e. if i replace Future<null> main() etc with void main() => runApp(new App()); the app runs fine. I've followed install instructions from the link provided and tried pasting in all the example incode in place of mine but still get the errors

My main.dart:

  import 'package:flutter/material.dart';
  import 'Localisations.dart';
  import 'package:flutter_localizations/flutter_localizations.dart';
  import 'dart:async';
  import 'package:camera/camera.dart';

  List<CameraDescription> cameras;

  Future<Null> main() async {
    cameras = await availableCameras();
    runApp(new App());
  }

  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      var statusBarHeight = MediaQuery.of(context).padding.top;
      var titleText = new Text(Localize.of(context).appTitle,
          textAlign: TextAlign.center,
          style: new TextStyle(fontFamily: 'CallingAngelsPersonalUse',
          fontSize: 50.0,
          color: Colors.black)
      );
      var backgroundImage = new BoxDecoration(
          image: new DecorationImage(
           image: new AssetImage('assets/background.png'),
          fit: BoxFit.cover,
        ),
      );

      var mainContainer = new Container(
        padding: EdgeInsets.only(top: statusBarHeight),
        height: double.infinity,
        width: double.infinity,
        decoration: backgroundImage,
        child: new Column(
          children: <Widget>[
                new Container(
                  margin: EdgeInsets.only(top: 10.0),
                  child: titleText
            )
          ],
        ),

      );

      return new Scaffold(
        body: mainContainer,
      );
    }
  }

  class App extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return new MaterialApp(
        onGenerateTitle: (BuildContext context) => Localize.of(context).appTitle,
        localizationsDelegates: [
          const LocalizeDelegate(),
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        supportedLocales: [
          const Locale('en', '')
        ],
        home: new MyApp(),
      );
    }
  }

My pubspec.yml:

dependencies:
  flutter:
    sdk: flutter
  camera: ^0.1.2
  path_provider: ^0.4.0
  flutter_localizations:
      sdk: flutter

This is my first real flutter app so any help would be appreciated.

Thanks

Upvotes: 4

Views: 15100

Answers (3)

zhangzhe
zhangzhe

Reputation: 1

  1. For android, make sure your midSdkVersion parameter is 21 or higher,
  2. if your project minSdkVersion == flutter.minSdkVersion
  3. edit .../packages/flutter_tools/gradle/src/main/groovy/flutter.groovy, change minSdkVersion value to 21 or higher

Take a picture using the camera

Upvotes: 0

Rohit Chaurasiya
Rohit Chaurasiya

Reputation: 607

First check your app.build minSdkVersion. Make sure minSdkVersion is 21.

minSdkVersion 21

Here is my code example:

List<CameraDescription> cameras = [];
main() async{
  WidgetsFlutterBinding.ensureInitialized();
  try {
    cameras = await availableCameras();

  } on CameraException catch (e) {
   logError(e.code, e.description);
  }
  runApp(MyApp());
}

Upvotes: 8

Marty
Marty

Reputation: 621

Despite manually adding the dependency to pubspec.yaml and running flutter pub get, what worked for me was running

flutter pub add camera

followed by

flutter pub get

and finally adding

import 'package:camera/camera.dart';

in main.dart (or the dart file where the error is happening). These are from the installation instructions.

Upvotes: 0

Related Questions