我是一名新开发人员,刚刚开始使用flutter。我正在尝试制作相机应用,并且遵循了本教程:
https://www.youtube.com/watch?v=_nS00ZKnINQ
我收到一个错误:Instance member 'takePicture' can't be accessed using static access.
您可以在此代码块中查看整个文件。请不要犹豫,询问您是否需要整个项目目录。
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
class CameraScreen extends StatefulWidget {
@override
_CameraScreenState createState() => _CameraScreenState();
}
class _CameraScreenState extends State<CameraScreen> {
CameraController cameraController;
List cameras;
int selectedCameraIndex;
String imgPath;
Future _initCameraController(CameraDescription cameraDescription) async {
if (cameraController != null) {
await cameraController.dispose();
}
cameraController =
CameraController(cameraDescription, ResolutionPreset.high);
cameraController.addListener(() {
if (mounted) {
setState(() {});
}
});
if (cameraController.value.hasError) {
print('!!!Camera error!!! ${cameraController.value.errorDescription}');
}
try {
await cameraController.initialize();
} on CameraException catch (e) {
_showCameraException(e);
}
if (mounted) {
setState(() {});
}
}
/// Display Camera Preview
Widget _cameraPreviewWidget() {
if (cameraController == null || cameraController.value.isInitialized) {
return const Text(
'Loading...',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.w900,
),
);
}
return AspectRatio(
aspectRatio: cameraController.value.aspectRatio,
child: CameraPreview(cameraController),
);
}
/// Display control bar with buttons to take picture
Widget _cameraControlWidget(context) {
return Expanded(
child: Align(
alignment: Alignment.center,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
FloatingActionButton(
child: Icon(
Icons.camera,
color: Colors.black,
),
backgroundColor: Colors.white,
onPressed: () {
_onCapturePressed(context);
})
],
),
));
}
/// Display a row of toggles to select the camera
Widget _cameraToggleRowWidget() {
if (cameras == null || cameras.isEmpty) {
return Spacer();
}
CameraDescription selectedCamera = cameras[selectedCameraIndex];
CameraLensDirection lensDirection = selectedCamera.lensDirection;
return Expanded(
child: Align(
alignment: Alignment.centerLeft,
child: FlatButton.icon(
onPressed: _onSwitchCamera,
icon: Icon(
_getCameraLensIcon(lensDirection),
color: Colors.white,
size: 24,
),
label: Text(
'${lensDirection.toString().substring(lensDirection.toString().indexOf('.') + 1).toUpperCase()}',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w500,
),
)),
));
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return null;
}
void _onSwitchCamera() {
selectedCameraIndex =
selectedCameraIndex < cameras.length - 1 ? selectedCameraIndex + 1 : 0;
CameraDescription selectedCamera = cameras[selectedCameraIndex];
_initCameraController(selectedCamera);
}
IconData _getCameraLensIcon(CameraLensDirection lensDirection) {
switch (lensDirection) {
case CameraLensDirection.back:
return CupertinoIcons.switch_camera;
case CameraLensDirection.front:
return CupertinoIcons.switch_camera_solid;
case CameraLensDirection.external:
return CupertinoIcons.photo_camera;
default:
return Icons.device_unknown;
}
}
}
void _onCapturePressed(context) async {
try {
final path =
join((await getTemporaryDirectory()).path, '${DateTime.now()}.png)');
await CameraController.takePicture(path);
} catch (e) {
_showCameraException(e);
}
}
void _showCameraException(CameraException e) {
String errorText = 'Error: ${e.code} \n Error Message: ${e.description}';
print(errorText);
}
我正在Windows 10上的Flutter中使用VS Code
答案 0 :(得分:0)
不要将 <TextInput
placeholder={"A Variable"}
onChangeText={(text) => this.setState({a: text}) }
value={this.state.a}
/>
作为静态方法调用。您必须使用初始化的控制器:
takePicture
要访问它,请将您的方法 cameraController.takePicture(path);
移动到您的类主体中。可以在 here 中找到您教程中的工作代码。