flutter 正在初始化Future中可用摄像机列表的摄像机

2wnc66cl  于 2022-11-30  发布在  Flutter
关注(0)|答案(4)|浏览(267)

我使用的代码是直接从www.example.com获得pub.dev,用于初始化摄像头和创建可用摄像头列表
这个列表是在Future main()函数中创建的,但是当我导航到CameraApp页面时,它没有被自动调用。有人遇到过这个问题吗?当我导航到这个代码的页面时,我如何初始化相机并创建可用相机的列表?请帮助,谢谢。

/// CameraApp is the Main Application.
class CameraApp extends StatelessWidget {
  /// Default Constructor
  const CameraApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: CameraExampleHome(),
    );
  }
}

List<CameraDescription> _cameras = <CameraDescription>[];

Future<void> main() async {
  // Fetch the available cameras before initializing the app.
  try {
    WidgetsFlutterBinding.ensureInitialized();
    _cameras = await availableCameras();
  } on CameraException catch (e) {
    _logError(e.code, e.description);
  }
  runApp(const CameraApp());
}

这是我从按钮中调用CameraApp函数的代码:

ElevatedButton(
              onPressed: () 
              {Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => CameraApp()));},
              child: const Text('Camera'),
              style: ElevatedButton.styleFrom(
                  minimumSize: const Size(160.0, 35.0)),

            ),
46qrfjad

46qrfjad1#

您可以创建一种单例来管理摄像机操作。

class CameraManager {
  // Declare your camera list here
  List<CameraDescription> _cameras = <CameraDescription>[];

  // Constructor
  CameraManager._privateConstructor() {}

  // initialise instance
  static final CameraManager instance =
      CameraManager._privateConstructor();

  // Add a getter to access camera list
  List<CameraDescription> get cameras => _cameras;

  // Init method
  init() async {
    try {
      _cameras = await availableCameras();
    } on CameraException catch (e) {
      _logError(e.code, e.description);
    }
  }

  // other needed methods to manage camera
  ...
}

然后在主函数中

Future<void> main() async {
  // Fetch the available cameras before initializing the app.
  try {
    WidgetsFlutterBinding.ensureInitialized();
    await CameraManager.instance.init();
  }
  runApp(const CameraApp());
}

然后,在应用程序的其他部分,您可以导入单例并使用CameraManager.instance.*访问方法和属性,例如CameraManager.instance.cameras通过getter访问_cameras

qyzbxkaa

qyzbxkaa2#

这里有几件事需要考虑..您所做的实现是正确的,但是您将摄像头命名为私有变量,通过添加_ like _camera可以在单个dart文件中访问该变量。删除该变量将使其在所有类中全局可用,只需导入main.dart即可
下面是完整的代码

主省道

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:trial/CameraExampleHome.dart';

List<CameraDescription> cameras = <CameraDescription>[];
void main() async {
  try {
    WidgetsFlutterBinding.ensureInitialized();
    cameras = await availableCameras();
    print(cameras);
  } on CameraException catch (e) {
    print(e.toString());
  }
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: CameraApp());
  }
}

class CameraApp extends StatelessWidget {
  /// Default Constructor
  const CameraApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: CameraExampleHome(),
    );
  }
}

照相机示例Home.dart

import 'package:flutter/material.dart';

import 'main.dart';

class CameraExampleHome extends StatefulWidget {
  const CameraExampleHome({Key? key}) : super(key: key);

  @override
  State<CameraExampleHome> createState() => _CameraExampleHomeState();
}

class _CameraExampleHomeState extends State<CameraExampleHome> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text("Available Cameras $cameras"),
//output: Available Cameras [CameraDescription(0, CameraLensDirection.back, 90), CameraDescription(1, CameraLensDirection.front, 270), CameraDescription(2, CameraLensDirection.front, 270)]
      ),
    );
  }
}
v1l68za4

v1l68za43#

可以为全局变量创建库文件。
在lib文件夹中创建名为“globals.dart”的文件。
在顶部声明以下行。

library your_project_name.globals;

然后在其中设置变量

List<CameraDescription> cameras = <CameraDescription>[];

主要用途

import 'globals.dart' as globals;

main() {
  ...
  globals.cameras = await availableCameras();
  ...
}

然后只需在项目中的任何位置使用该变量即可。

import 'globals.dart' as globals;

globals.cameras...
f1tvaqid

f1tvaqid4#

如下声明CameraDescription全局示例列表:

List<CameraDescription> cameras = <CameraDescription>[];

现在,您可以通过导入main来访问camera示例。dart,_ always将示例变量设置为private,该变量在文件外部不可访问

相关问题