小编Bas*_*mad的帖子

如何解决Flutter中的LateInitializationError?

我遇到以下问题,希望有人可以帮助我。我尝试使用以下代码将相机包含在我的 flutter 应用程序中:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';


class Route02 extends StatefulWidget {
  @override
  CameraState createState() => CameraState();
}

class CameraState extends State<Route02> {
  late List<CameraDescription> cameras;
  late CameraController _controller;
  bool isReady = false;

  @override
  void initState() {
    super.initState();
    setupCameras();
  }

  Future<void> setupCameras() async {
    try {
      cameras = await availableCameras();
      _controller = new CameraController(cameras[0], ResolutionPreset.ultraHigh);
      await _controller.initialize();
    } on CameraException catch () {
      setState(() {
        isReady = false;
      });
    }
    setState(() {
      isReady = true;
    });
  }

  Widget …
Run Code Online (Sandbox Code Playgroud)

error-handling exception solution dart flutter

5
推荐指数
1
解决办法
5791
查看次数

标签 统计

dart ×1

error-handling ×1

exception ×1

flutter ×1

solution ×1