1

I am developing an application for shopping using flutter, so I am stacking somewhere and I need help.

I use image_picker code provided on pub.dev (https://pub.dev/packages/image_picker#-readme-tab-) then I developed a page for adding products on my app, so when I click on the camera icon or gallery icon to pick image the app crashes and opens the camera screen/gallery screen.

my big problem is; it works fine with the emulator but on a real phone, it crushes.

I tried to use retrieveLostData() as shown on pub.dev, but I didn't know where to use it.

Bellow is my code

import 'package:flutter/material.dart';
import 'dart:io';
import 'package:flutter/widgets.dart';

import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:firebase_storage/firebase_storage.dart';

//my imports
import 'package:myshop/main.dart';

class ImageCapture extends StatefulWidget {
  @override
  _ImageCaptureState createState() => _ImageCaptureState();
}

class _ImageCaptureState extends State<ImageCapture> {
  File _imageFile;

  Future<void> _cropImage() async {
    File cropped = await ImageCropper.cropImage(
        sourcePath: _imageFile.path,);

    setState(() {
      _imageFile = cropped ?? _imageFile;
    });
  }
 // pick image from galery or camera
  Future<void> _pickImage(ImageSource source) async {
    File selected = await ImagePicker.pickImage(source: source);

    setState(() {
      _imageFile = selected;
    });
  }
   //remove image
  void _clear() {
    setState(() => _imageFile = null);
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        backgroundColor: Colors.orange[700],
        title: Text(
          'Image Capture',
          style: TextStyle(fontFamily: 'Exo', fontSize: 13.0),
        ),
        actions: <Widget>[
          IconButton(
            icon: Icon(
              Icons.search,
              color: Colors.white,
              size: 23.0,
            ),
            onPressed: () {},
          ),
          IconButton(
              icon: Icon(
                Icons.home,
                size: 18,
                color: Colors.white,
              ),
              onPressed: () => Navigator.of(context)
                  .push(MaterialPageRoute(builder: (context) => Home())))
        ],
      ),

      //pick image from camera or gallery
      bottomNavigationBar: BottomAppBar(
        color: Colors.cyan[900],
        child: Container( margin: EdgeInsets.fromLTRB(30, 0, 30, 0),
          child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[

              IconButton(
                icon: Icon(Icons.photo_camera, color: Colors.orange, size: 18,),
                onPressed: () => _pickImage(ImageSource.camera),
              ),
              IconButton(
                icon: Icon(Icons.photo_library, color: Colors.orange, size: 18,),
                onPressed: () => _pickImage(ImageSource.gallery),
              ),
            ],
          ),
        ),
      ),

      body: ListView(
        children: <Widget>[
          if (_imageFile != null) ...[
            Image.file(_imageFile),
            Row( mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                FlatButton(
                  child: Icon(Icons.crop, size: 18,),
                  onPressed: _cropImage,
                ),
                FlatButton(
                  child: Icon(Icons.refresh,size: 18,),
                  onPressed: _clear,
                ),
              ],
            ),
          ], if (_imageFile==null)...[
            Center(
              child: Text('No Image Captured', style: TextStyle(color: Colors.black54),),
            )
          ]
        ],
      ),
    );
  }
}
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Bethson
  • 23
  • 1
  • 5

1 Answers1

5

You need to add the camera and storage permission in the AndroidManifest.xml file of your project.

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

also, you need to check for runtime permission for both in your activity.

Head to Storage permission error in Marshmallow for more information.

OMi Shah
  • 5,768
  • 3
  • 25
  • 34