I am working on a Timeclock app. I have everything working, except for capturing a photo and sending it to my backend API (Not Firebase or similar). For the moment, I have my API grabbing an image from an IP Camera near the tablet. But, I would prefer to use the camera on the tablet. I am trying to avoid using the default camera interface, which requires too much interraction from the user. Plus, all of the How To Tutorials on how to do this seem to beout of date and no longer work with the current version of FF. I tried to write a custom widget to handle this for me but getting an error I do not know how to handle.
Can someone point me in the right direction for resolving this code error?
The error I am getting, and the line of code it applies to, is :
Too many positional arguments: 0 expected, but 1 found.
Try removing the extra arguments.dartextra_positional_arguments
await _controller.takePicture(imagePath);
// Automatic FlutterFlow imports
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/widgets/index.dart'; // Imports other custom widgets
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom widget code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'dart:io';
import 'package:camera/camera.dart';
import 'package:path_provider/path_provider.dart';
class CameraScreen extends StatefulWidget {
const CameraScreen({
Key? key,
this.width,
this.height,
}) : super(key: key);
final double? width;
final double? height;
@override
_CameraScreenState createState() => _CameraScreenState();
}
class _CameraScreenState extends State<CameraScreen> {
late CameraController _controller;
late Future<void> _initializeControllerFuture;
String? _imagePath;
@override
void initState() {
super.initState();
_initializeCamera();
}
Future<void> _initializeCamera() async {
List<CameraDescription> cameras = await availableCameras();
CameraDescription frontCamera = cameras.firstWhere(
(camera) => camera.lensDirection == CameraLensDirection.front,
orElse: () => cameras.first,
);
_controller = CameraController(
frontCamera,
ResolutionPreset.medium,
);
_initializeControllerFuture = _controller.initialize();
}
Future<void> _takePicture() async {
if (!_controller.value.isInitialized) {
return;
}
try {
await _initializeControllerFuture;
if (!_controller.value.isInitialized) {
return;
}
final Directory extDir = await getApplicationDocumentsDirectory();
final String imagePath = '${extDir.path}/image_${DateTime.now()}.png';
await _controller.takePicture(imagePath);
setState(() {
_imagePath = imagePath;
});
} catch (e) {
print("Error capturing image: $e");
}
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Camera')),
body: FutureBuilder<void>(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return CameraPreview(_controller);
} else {
return Center(child: CircularProgressIndicator());
}
},
),
floatingActionButton: FloatingActionButton(
onPressed: _takePicture,
child: Icon(Icons.camera),
),
);
}
}