0

Iam opening the camera directly when the user enters the page. It opens and Iam able to take an image and display it. But I get the error " type 'Future' is not a subtype of type 'Widget' " before the camera opens and when I close it to display the taken image instead of the camera. I know it is because Iam using an async function to open the camera and get the image file. But Iam not able to get around this error. Can someone help me with this problem?

Here is my Code in a Stateful widget:

  File _imageFile;

  _getLocalImage() async {
final imageFile = await ImagePicker().getImage(source: ImageSource.camera, imageQuality: 100);
if (imageFile != null) {
  setState(() {
    _imageFile = File(imageFile.path);
  });
} else {
  Navigator.of(context).pop();
}
  }

  @override
  Widget build(BuildContext context) {

double _width = MediaQuery.of(context).size.width;
double _height = MediaQuery.of(context).size.height;

return _imageFile == null? _getLocalImage() :
Image.file(
  _imageFile,
  fit: BoxFit.cover,
  height: _height - 20,
  width: _width,
);
  }
Kinnay
  • 79
  • 7
  • 2
    Because the function `_getLocalImage()` does not return a Widget. Since getting the image is asynchronous, try out `FutureBuilder` for this case https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html – kounex Oct 26 '20 at 11:21
  • Can you give me an exmaple of how I can fit the imagePicker inside the FutureBuilder. Because the future and AsyncSnapshot confuses me – Kinnay Oct 26 '20 at 12:42

0 Answers0