不可为 null 的变量“cameras”;必须初始化

发布于 2025-01-09 14:03:40 字数 904 浏览 0 评论 0原文

当我尝试使用相机变量时,我的代码中出现以下错误。如何纠正这个问题。感谢您对此的帮助。

The non-nullable variable 'cameras' must be initialized.

CameraScreen.dart

 import 'package:camera/camera.dart';
    import 'package:flutter/material.dart';
    
    
    List <CameraDescription> cameras;
    
    class CameraScreen extends StatefulWidget {
      const CameraScreen({Key? key}) : super(key: key);
    
      @override
      _CameraScreenState createState() => _CameraScreenState();
    }
    
    class _CameraScreenState extends State<CameraScreen> {
      @override
      Widget build(BuildContext context) {
        return Scaffold();
      }
    }

main.dartt

Future <void> main() async{
  WidgetsFlutterBinding.ensureInitialized();
  cameras =await availableCameras();
  runApp(const MyApp());
}

I'm getting below error in my code when I'm trying to use cameras variable. how to correct this. appreciate your help on this.

The non-nullable variable 'cameras' must be initialized.

CameraScreeen.dart

 import 'package:camera/camera.dart';
    import 'package:flutter/material.dart';
    
    
    List <CameraDescription> cameras;
    
    class CameraScreen extends StatefulWidget {
      const CameraScreen({Key? key}) : super(key: key);
    
      @override
      _CameraScreenState createState() => _CameraScreenState();
    }
    
    class _CameraScreenState extends State<CameraScreen> {
      @override
      Widget build(BuildContext context) {
        return Scaffold();
      }
    }

main.dartt

Future <void> main() async{
  WidgetsFlutterBinding.ensureInitialized();
  cameras =await availableCameras();
  runApp(const MyApp());
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

八巷 2025-01-16 14:03:40

这是由于 Flutter 和 Dart 健全的 null 安全特性。

变量现在不能为空,如果您想接受 null 值,则必须使用 ? 使它们可为空,

例如:

List? Camera;

如果您仍然不想使它们可为空,那么您可以使用 late 关键字,它允许我们稍后初始化值,但我们必须确保它之前已初始化正在某处使用。

例如:

late List相机;

This is due to the sound null safety feature of Flutter and Dart.

Variables cannot be null now if you want to accept them null values you have to make them nullable using ?

for example:

List <CameraDescription>? cameras;

If you still don't want to make them nullable then you can use a late keyword which allows us to initialize value later on but we have to make sure that it is initialized before is being used somewhere.

for example:

late List <CameraDescription> cameras;

酷遇一生 2025-01-16 14:03:40

使相机可变。
喜欢:-

List <CameraDescription>? cameras;

Make the cameras variable.
like:-

List <CameraDescription>? cameras;
感性不性感 2025-01-16 14:03:40

您不能在没有分配 dart null 安全性的情况下留下非空变量,不允许这样做。

您的全局变量相机未初始化

List<> camera;//for global it is not acceptable

you can make List<>? camera;// now it is acceptable but you must need to initialize first before using it other wise it will throw exception 

you can not leave a not null variable without assign dart null safety not allowing this..

Your Global variable camera is not initialized

List<> camera;//for global it is not acceptable

you can make List<>? camera;// now it is acceptable but you must need to initialize first before using it other wise it will throw exception 
夜光 2025-01-16 14:03:40

您可以将 cameras List 设置为可为空,如下所示:

List<CameraDescription>? cameras;

或者,如果您不想使其可为空,则可以创建一个空相机列表,如下所示:

List<CameraDescription> cameras = List<CameraDescription>.empty(growable: true);

You can either make the cameras List nullable like this:

List<CameraDescription>? cameras;

OR if you don't want to make it nullable then you can make an empty cameras list like this:

List<CameraDescription> cameras = List<CameraDescription>.empty(growable: true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文