Skip to content

Instantly share code, notes, and snippets.

@Alby-o
Last active February 8, 2019 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alby-o/a05a7dfdf24a034c6d8b671c7c115b63 to your computer and use it in GitHub Desktop.
Save Alby-o/a05a7dfdf24a034c6d8b671c7c115b63 to your computer and use it in GitHub Desktop.
Attempt to invoke virtual method 'int android.hardware.camera2.CameraCaptureSession.capture(android.hardware.camera2.CaptureRequest, android.hardware.camera2.CameraCaptureSession$CaptureCallback, android.os.Handler)' on a null object reference
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: new Camera(),
),
);
}
}
class Camera extends StatefulWidget {
@override
State<Camera> createState() => new _CameraState();
}
class _CameraState extends State<Camera> {
CameraController _controller;
List<CameraDescription> _cameras;
bool _runningErrorCreation = false;
int _count;
void _runErrorCreation() async {
if (!_runningErrorCreation) {
_count = 0;
_runningErrorCreation = true;
while (await _startImageStream()) {}
_runningErrorCreation = false;
}
}
Future<bool> _startImageStream() async {
_count++;
try {
Directory dir = await getTemporaryDirectory();
String path = join(dir.path, "capture.jpg");
File img = new File(path);
if (await img.exists()) {
await img.delete();
}
await _controller.startImageStream((_) {});
await _controller.stopImageStream();
await _controller.takePicture(path);
return true;
} catch (e) {
debugPrint(e.toString());
debugPrint("Times ran before error occurred: $_count");
return false;
}
}
@override
void initState() {
super.initState();
_getCameras().then((_) {
_initialise();
});
}
Future<void> _getCameras() async {
_cameras = await availableCameras();
}
Future<void> _initialise() async {
if (_cameras.isEmpty) {
setState(() {});
return;
}
CameraDescription camera;
for (int i = 0; i < _cameras.length; i++) {
if (_cameras[i].lensDirection == CameraLensDirection.front) {
camera = _cameras[i];
break;
}
}
if (camera == null) {
// In the case cameras for the given direction were found
camera = _cameras[0];
}
_controller = new CameraController(camera, ResolutionPreset.low);
await _controller.initialize();
if (!mounted) return;
setState(() {});
}
bool get isInitialised {
return _controller != null && _controller.value.isInitialized;
}
Widget get contents {
if (_cameras != null && _cameras.isEmpty) {
return new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
"No cameras found...",
),
new Text(
"Check that you are using a device with connected cameras.",
),
],
),
);
}
if (!isInitialised) {
return Container();
}
return new AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: CameraPreview(_controller),
);
}
Widget build(BuildContext context) {
return new GestureDetector(
onTap: _runErrorCreation,
child: contents,
);
}
}
name: tst
description: A new Flutter project.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
camera: 0.3.0+3
path_provider: ^0.4.1
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment