[英]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'),
)
],
),
),
),
),
);
}
}
我用圖像選擇器和圖像裁剪器包制作了一個簡單的應用程序,在編寫此代碼時出現此錯誤。 每當我嘗試運行此代碼時,它都會在我的設備上顯示“用於空值的空檢查運算符..”雖然我設置了狀態。 你能解決這個錯誤嗎? 我是新來的,我不知道為什么它會顯示這個錯誤..
而不是那個背景圖像: FileImage(imageFile!),
使用這個 backgroundImage: imageFile != null ? 文件圖像(圖像文件!):空,
因為您正在嘗試打印屏幕而不在 imageFile 中放置圖片
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.