Reputation: 79
I am trying to use the camera plugin for flutter.
I call availableCameras() in the main function of the app, as I have seen in documentation like this:
List<CameraDescription> cameras;
Future<void> main() async {
try {
cameras = await availableCameras();
} catch (e) {
print("Error: $e");
}
runApp(MyApp());
}
The try statement fails instantly printing
Error: Null check operator used on a null value
I call Home() in MyApp(), which has a tabbed navigator, which calls FaceAScreen() like this:
return Scaffold(
appBar: buildAppBar(),
bottomNavigationBar: BottomNavBar(tabController: _tabController, myTabs: myTabs),
body: TabBarView(
controller: _tabController,
children: <Widget>[Body(), FaceAScreen(cameras: widget.cameras)],
),
);
I want the Camera in the FaceAScreen() page, but it shows, expectedly, "No Camera!" text, since the List cameras
is null.
The code build method for FaceAScreen() is:
@override
Widget build(BuildContext context) {
if (!isDetecting) {
return Center(
child: Container(
margin: EdgeInsets.all(10),
child: Text("No cameras!"),
),
);
}
// This is not what causes the no camera to show on the page
if (!controller.value.isInitialized) {
return Container(child: Text("No cameras"));
}
return AspectRatio(
aspectRatio: controller.value.aspectRatio,
child: CameraPreview(controller),
);
}
I have looked at the null value error relentlessly and have upgraded my flutter package from the terminal many times, including running flutter clean. Flutter doctor shows everything is fine. I also have double checked the settings on my android emulator and have front facing set to webcam0 and back facing as "emulated". I checked opening the camera app in the emulator and that works. All the tutorials seem to write use the availableCameras() method exactly as I did (without the try catch even). Any help would be great, thanks!
Upvotes: 1
Views: 2994
Reputation: 6239
Recently I finished a tutorial, which uses camera and works fine. The code below can be of help to you:
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';
class CameraPage extends StatefulWidget {
CameraPage({Key key}) : super(key: key);
@override
_CameraPageState createState() => _CameraPageState();
}
/* ============================================================================================= */
class _CameraPageState extends State<CameraPage> {
List<CameraDescription> _cameras;
CameraController _controller;
var _isReady = false;
/* ---------------------------------------------------------------------------- */
@override
void initState() {
super.initState();
_setUpCamera();
}
/* ---------------------------------------------------------------------------- */
void _setUpCamera() async {
try {
// initialize cameras
_cameras = await availableCameras();
// initialize camera controllers
// Current bug for high/medium with Samsung devices
_controller = CameraController(_cameras[0], ResolutionPreset.medium);
await _controller.initialize();
} on CameraException catch (_) {
// do something on error
}
if (mounted) setState(() => _isReady = true);
}
/* ---------------------------------------------------------------------------- */
Widget cameraPreview() {
return AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: CameraPreview(_controller),
);
}
/* ---------------------------------------------------------------------------- */
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
floatingActionButton: getFooter(),
body: getBody(),
);
}
/* ---------------------------------------------------------------------------- */
Widget getBody() {
final size = MediaQuery.of(context).size;
var flag = !_isReady || _controller == null || !_controller.value.isInitialized;
return Container(
decoration: flag ? BoxDecoration(color: Colors.white) : null,
width: size.width,
height: size.height,
child: flag
? Center(
child: SizedBox(
width: 25,
height: 25,
child: CircularProgressIndicator(strokeWidth: 3),
),
)
: ClipRRect(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(10),
bottomRight: Radius.circular(10),
),
child: cameraPreview(),
),
);
}
.....
}
Obviously your Android device emulator must have enabled cameras and SD Card.
Upvotes: 1