简体   繁体   中英

I made a simple app with image picker and image cropper pakage and when writing this code i got this error, Null check operator used on a null value

    import 'dart:io';
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    import 'package:image_cropper/image_cropper.dart';
    import 'package:image_picker/image_picker.dart';
    
    class ProfilePage extends StatefulWidget {
      const ProfilePage({Key? key}) : super(key: key);
    
      @override
      State<ProfilePage> createState() => _ProfilePageState();
    }
    
    class _ProfilePageState extends State<ProfilePage> {
      File ?imageFile;
      TextEditingController fullnamecontroller=TextEditingController();
    
      void selectImage(ImageSource source)async {
       XFile?pickedFile= await ImagePicker().pickImage(source: source);
       if(pickedFile!=null){
         cropImage(pickedFile);
       }
      }
    
      void cropImage(XFile file)async{
        CroppedFile?croppedImage= await ImageCropper().cropImage(sourcePath: file.path,aspectRatio: CropAspectRatio(ratioX: 1, ratioY: 1),compressQuality: 20
      );
      if (croppedImage!=null){
        setState((){
          imageFile=croppedImage as File?;
        });
      }
      }
      void ShowphotoOption(){
        showDialog(context: context, builder: (context){return AlertDialog(
          title: Text('Select Image'),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
            ListTile(
              onTap: (){selectImage(ImageSource.gallery);
                Navigator.pop(context);},
              leading: Icon(Icons.photo_album),
              title: Text('Selct from gallery'),
    
    
            ),
            ListTile(
              onTap: (){selectImage(ImageSource.camera);
                Navigator.pop(context);},
              leading: Icon(Icons.camera_alt),
              title: Text('Take photo from camera'),
    
            )
          ],),
        );});
      }
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            centerTitle: true,
              title:Text('Complete Your Profile')
    
          ),
          body: SafeArea(
            child: Container(
              child: Padding(
                padding: const EdgeInsets.all(20),
                child: ListView(
                  children: [
                    CupertinoButton(
                      padding: EdgeInsets.all(0),
                      onPressed: (){
                        ShowphotoOption();
                      },
                      child: CircleAvatar(
                        backgroundImage: FileImage(imageFile!),
                        child: Icon(Icons.person,size: 60,),
                        radius: 60,
                      ),
                    ),
                    SizedBox(height: 10,),
                    TextField(
                      decoration: InputDecoration(
                        labelText: 'Full name',
                      ),
                    ),
                    SizedBox(height: 20,),
                    CupertinoButton(
                      onPressed: (){},
                      color: Colors.lightBlue,
                      child: Text('Submit'),
                    )
                  ],
                ),
              ),
    
            ),
          ),
        );
      }
    }

I made a simple app with image picker and image cropper pakage and when writing this code i got this error. Whenever I try to run this code, it shows on my device "Null check operator used on a null value.." Although I set the state. Can you please solve this error? I am new to flutter, I dont know why it shows this error..

Instead of that backgroundImage: FileImage(imageFile!),
Use this backgroundImage: imageFile != null ? FileImage(imageFile!) : null,

Because you're trying to print the screen without putting a picture in the imageFile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM