Flutter Camera Plugin: Unable to Open Front-Facing Camera

I’m working on a Flutter project where I need to open the front-facing camera, but no matter what I try, the camera always starts facing the back. I’m unfamiliar with the project, and I’m having trouble figuring out how to get the front-facing camera to open by default.

I’m running that getCameras() function and when that completes it should update my cameras list. Then setting my current camera to the one with front facing camera. I also tried choosing a specific index from the list but it was only ever the back camera

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class _CameraState extends State<Camera>
with WidgetsBindingObserver, SingleTickerProviderStateMixin {
final GlobalKey<ConvexAppBarState> appBarKey;
_CameraState({this.appBarKey});
//Global Scaffold Key
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
FlutterSecureStorage storage = FlutterSecureStorage();
//Video and Camera variables
CameraController cameraController;
VideoPlayerController videoController;
VoidCallback videoPlayerListener;
XFile videoFile;
String uploadVideoPath;
bool enableAudio = true;
double _minAvailableExposureOffset = 0.0;
double _maxAvailableExposureOffset = 0.0;
double _currentExposureOffset = 0.0;
double _minAvailableZoom = 1.0;
double _maxAvailableZoom = 1.0;
double _currentScale = 1.0;
double _baseScale = 1.0;
int _pointers = 0;
bool _recordFirstPress;
bool showPath = true;
Timer _countdownTimer;
bool showCounter;
int _start = 3;
bool changeIcon;
AnimationController _animationController;
List<CameraDescription> cameras = [];
bool _cameraOn = true;
CameraDescription currentCamera;
//Video Upload Variable
final ImagePicker _picker = ImagePicker();
//Spoon Variables
bool _spoonActive;
double circles = 1.0;
bool spoonButtonUpdate;
bool showMessage;
double spreadRadius;
Color spreadColor;
List<SpoonSet> spoonSets = [];
SpoonSet currentSet = SpoonSet();
String _templateValue = 'Standard Template';
List<DropdownMenuItem> _templates = [];
List<Widget> _spoonWidgets = [];
List<dynamic> _spoonQuestions;
List<Widget> dots = [];
int currentSpoon = 1;
//Initialize screen
@override
void initState() {
super.initState();
showMessage = true;
spoonButtonUpdate = false;
checkSpoonAlert();
spreadRadius = 0;
spreadColor = Colors.transparent;
showCounter = false;
changeIcon = false;
WidgetsBinding.instance.addObserver(this);
_animationController = AnimationController(
vsync: this,
duration: Duration(seconds: 120),
)..addStatusListener((status) {
if (status == AnimationStatus.completed) {
onStopButtonPressed();
setState(() {
_animationController.value = 0;
});
}
});
_animationController.value = 0.0;
_recordFirstPress = true;
_spoonActive = false;
loadSpoon();
Future.delayed(Duration(milliseconds: 1000)).whenComplete(() {
setState(() {
spoonButtonUpdate = !spoonButtonUpdate;
spreadRadius = 2.0;
spreadColor = Colors.white.withOpacity(0.8);
});
});
getCameras().whenComplete(() {
currentCamera = cameras.firstWhere(
(camera) => camera.lensDirection == CameraLensDirection.front);
onNewCameraSelected(currentCamera);
});
}
</code>
<code>class _CameraState extends State<Camera> with WidgetsBindingObserver, SingleTickerProviderStateMixin { final GlobalKey<ConvexAppBarState> appBarKey; _CameraState({this.appBarKey}); //Global Scaffold Key final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>(); FlutterSecureStorage storage = FlutterSecureStorage(); //Video and Camera variables CameraController cameraController; VideoPlayerController videoController; VoidCallback videoPlayerListener; XFile videoFile; String uploadVideoPath; bool enableAudio = true; double _minAvailableExposureOffset = 0.0; double _maxAvailableExposureOffset = 0.0; double _currentExposureOffset = 0.0; double _minAvailableZoom = 1.0; double _maxAvailableZoom = 1.0; double _currentScale = 1.0; double _baseScale = 1.0; int _pointers = 0; bool _recordFirstPress; bool showPath = true; Timer _countdownTimer; bool showCounter; int _start = 3; bool changeIcon; AnimationController _animationController; List<CameraDescription> cameras = []; bool _cameraOn = true; CameraDescription currentCamera; //Video Upload Variable final ImagePicker _picker = ImagePicker(); //Spoon Variables bool _spoonActive; double circles = 1.0; bool spoonButtonUpdate; bool showMessage; double spreadRadius; Color spreadColor; List<SpoonSet> spoonSets = []; SpoonSet currentSet = SpoonSet(); String _templateValue = 'Standard Template'; List<DropdownMenuItem> _templates = []; List<Widget> _spoonWidgets = []; List<dynamic> _spoonQuestions; List<Widget> dots = []; int currentSpoon = 1; //Initialize screen @override void initState() { super.initState(); showMessage = true; spoonButtonUpdate = false; checkSpoonAlert(); spreadRadius = 0; spreadColor = Colors.transparent; showCounter = false; changeIcon = false; WidgetsBinding.instance.addObserver(this); _animationController = AnimationController( vsync: this, duration: Duration(seconds: 120), )..addStatusListener((status) { if (status == AnimationStatus.completed) { onStopButtonPressed(); setState(() { _animationController.value = 0; }); } }); _animationController.value = 0.0; _recordFirstPress = true; _spoonActive = false; loadSpoon(); Future.delayed(Duration(milliseconds: 1000)).whenComplete(() { setState(() { spoonButtonUpdate = !spoonButtonUpdate; spreadRadius = 2.0; spreadColor = Colors.white.withOpacity(0.8); }); }); getCameras().whenComplete(() { currentCamera = cameras.firstWhere( (camera) => camera.lensDirection == CameraLensDirection.front); onNewCameraSelected(currentCamera); }); } </code>
class _CameraState extends State<Camera>
    with WidgetsBindingObserver, SingleTickerProviderStateMixin {
  final GlobalKey<ConvexAppBarState> appBarKey;
  _CameraState({this.appBarKey});
  //Global Scaffold Key
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  FlutterSecureStorage storage = FlutterSecureStorage();

  //Video and Camera variables
  CameraController cameraController;
  VideoPlayerController videoController;
  VoidCallback videoPlayerListener;
  XFile videoFile;
  String uploadVideoPath;
  bool enableAudio = true;
  double _minAvailableExposureOffset = 0.0;
  double _maxAvailableExposureOffset = 0.0;
  double _currentExposureOffset = 0.0;
  double _minAvailableZoom = 1.0;
  double _maxAvailableZoom = 1.0;
  double _currentScale = 1.0;
  double _baseScale = 1.0;
  int _pointers = 0;
  bool _recordFirstPress;
  bool showPath = true;
  Timer _countdownTimer;
  bool showCounter;
  int _start = 3;
  bool changeIcon;
  AnimationController _animationController;
  List<CameraDescription> cameras = [];
  bool _cameraOn = true;
  CameraDescription currentCamera;
  //Video Upload Variable
  final ImagePicker _picker = ImagePicker();

  //Spoon Variables
  bool _spoonActive;
  double circles = 1.0;
  bool spoonButtonUpdate;
  bool showMessage;
  double spreadRadius;
  Color spreadColor;
  List<SpoonSet> spoonSets = [];
  SpoonSet currentSet = SpoonSet();
  String _templateValue = 'Standard Template';
  List<DropdownMenuItem> _templates = [];
  List<Widget> _spoonWidgets = [];
  List<dynamic> _spoonQuestions;
  List<Widget> dots = [];
  int currentSpoon = 1;

  //Initialize screen
  @override
  void initState() {
    super.initState();
    showMessage = true;
    spoonButtonUpdate = false;
    checkSpoonAlert();
    spreadRadius = 0;
    spreadColor = Colors.transparent;
    showCounter = false;
    changeIcon = false;
    WidgetsBinding.instance.addObserver(this);
    _animationController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 120),
    )..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          onStopButtonPressed();
          setState(() {
            _animationController.value = 0;
          });
        }
      });
    _animationController.value = 0.0;
    _recordFirstPress = true;
    _spoonActive = false;

    loadSpoon();

    Future.delayed(Duration(milliseconds: 1000)).whenComplete(() {
      setState(() {
        spoonButtonUpdate = !spoonButtonUpdate;
        spreadRadius = 2.0;
        spreadColor = Colors.white.withOpacity(0.8);
      });
    });

    getCameras().whenComplete(() {
      currentCamera = cameras.firstWhere(
          (camera) => camera.lensDirection == CameraLensDirection.front);
      onNewCameraSelected(currentCamera);
    });
  }

New contributor

Jess_c_lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật