Home > database >  After Adding image picker my images are not printed in flutter
After Adding image picker my images are not printed in flutter

Time:08-24

After Adding the image picker my images are not printed in a flutter the screen is blank. I added import 'dart: io'; and import 'package:image_picker/image_picker.dart'; My image picker is in the alert dialog box, but when I choose an image from a file or gallery, it doesn't appear on that screen.

  GestureDetector(
                        onTap: () async {
                          await Permission.photos.request();
                          var status = await Permission.photos.status;
                          if (status.isGranted) {
                            File _pickedImage = await ImagePicker().pickImage(
                              source: ImageSource.gallery,
                              maxWidth: 100,
                              maxHeight: 100,
                            ) as File;
                            if (_pickedImage != null) {
                              setState(() {
                                pickedImage = _pickedImage;
                              });
                            }
                          } else if (status.isDenied ||
                              status.isPermanentlyDenied) {
                            showDialog(
                                context: context,
                                builder: (context) {
                                  if (Platform.isAndroid)
                                    return AlertDialog(
                                      title: Text('Permission Not Granted'),
                                      content: Text(
                                          'The permission for photo library is not granted'),
                                      actions: [
                                        FlatButton(
                                          onPressed: () => openAppSettings(),
                                          child: Text('Ok'),
                                        ),
                                      ],
                                    );
                                  return CupertinoAlertDialog(
                                    title: Text('Permission Not Granted'),
                                    content: Text(
                                        'The permission for photo library is not granted'),
                                    actions: [
                                      FlatButton(
                                        onPressed: () => openAppSettings(),
                                        child: Text('Ok'),
                                      ),
                                    ],
                                  );
                                });
                          }
                        },
                        child: Padding(
                          padding: EdgeInsets.only(
                            top: 10.0,
                            bottom: 10.0,
                          ),
                          child: Container(
                            height: 100.0,
                            width: 100.0,
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(10.0),
                              color: Colors.transparent,
                              border: Border.all(
                                style: BorderStyle.solid,
                              ),
                            ),
                            child: pickedImage == null
                                ? Icon(
                                    Icons.add,
                                    color: Colors.black,
                                  )
                                : Image.file(
                                    pickedImage,
                                    fit: BoxFit.contain,
                                  ),
                          ),
                        ),
                      ),

I added dependencies: image_picker: ^0.8.5 3

CodePudding user response:

Where you're printing the image, try this:

import 'dart:io';
...
...    
Image.file(
   File(pickedImage.path),
   fit: BoxFit.contain,
)

The image you're trying to print is a file, so you need to obtain the path and print it using the File() class. You can i

  • Related