-1

I am new with Flutter, I am trying to open camera, for this I followed example code but when ever I debug my app it gives me Exception as shown in image Error Image

Here's my code:

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

List<CameraDescription> cameras;

Future<Null> main() async {
  try {
    cameras = await availableCameras();
  } on CameraException catch (e) {
    print('Error: $e.code\nError Message: $e.message');
  }
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'RealTime Detection',
      home: HomePage(cameras),
    );
  }
}
Talha Nadeem
  • 99
  • 1
  • 4
  • 22

1 Answers1

2

you can do something like this:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) => MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'RealTime Detection',
        home: FutureBuilder<List<CameraDescription>>(
          future: availableCameras(),
          builder: (
            BuildContext context,
            AsyncSnapshot<List<CameraDescription>> snapshot,
          ) {
            if (snapshot.hasData) {
              return HomePage(snapshot.data);
            } else {
              return CircularProgressIndicator();
            }
          },
        ),
      );
}
Hamed
  • 5,867
  • 4
  • 32
  • 56