I have recently migrated my flutter app to null safety 2.12.0 , I had to do a lot of changes to my code and I'm only stuck at my image picker and upload functions. I manage to change the null accepted variables. application runs without an issue but after image has been picked its not shown in the ui or go down for other pipe line functions that requires the image file data. I pretty new to flutter and this sudden changes to flutter caused much confusion. I would much appreciate the solution for this and a good explanation of avoiding such problems in the future, thank you !
Git of entire code "https://github.com/sudaraka93/stack.git"
late SharedPreferences sharedPreferences;
late Size deviceSize;
Future<File>? file;
late String base64Image;
String status = '';
late File tmpFile;
String errMessage = 'Error Uploading Image';
DateTime selectedDate = DateTime.now();
final TextEditingController CommentController = new TextEditingController();
@override
void initState() {
}
chooseImage() {
setState((){
file = ImagePicker().getImage(source: ImageSource.gallery) as Future<File>;
});
}
startUpload()async {
setStatus('Uploading Image...');
if (null == tmpFile) {
setStatus(errMessage);
return;
}
String fileName = tmpFile.path.split('/').last;
upload(base64Image);
}
Widget showImage() {
return FutureBuilder<File>(
future: file,
builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
if (snapshot.connectionState == ConnectionState.done &&
null != snapshot.data) {
tmpFile = snapshot.data!;
base64Image = base64Encode(snapshot.data!.readAsBytesSync());
return Flexible(
child:Container(
child:Image.file(
snapshot.data!,
fit: BoxFit.fill,
),
)
);
} else if (null != snapshot.error) {
return const Text(
'画像の選択エラー',
textAlign: TextAlign.center,
);
} else {
return const Text(
'画像が選択されていません',
textAlign: TextAlign.center,
);
}
},
);
}