I am trying to work with a code where I want it to check if my file is not empty. When I try to do this:
void uploadProduct() {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
if (image != null) {
setState(() {
image = null;
});
_formKey.currentState!.reset();
} else {
snackBar('Please Pick Image', context);
}
} else {
snackBar('Fields must not be empty', context);
}
}
What I am trying is to check whether or not if I have the file(image) selected or not, when I don't have any image selected, it should show the snackBar (please pick image) as stated in the else, but it doesn't work.
How can i fix it?
Check for value is null or not
if(_image != null)
If you have the path of the file, you can use dart:io
var file = File('the_path_to_the_image');
print(file.lengthSync());
now check if length > 0 then file is not empty otherwise file is empty
File not being empty isn't very clear. There might be two conditions:
File is null
You can check it by:
if(_image != null)
If the file exists but it's empty
You can check that by:
final _imageContent = await _image.readAsString();
if(_imagContent.length > 0)
Related
Hi so I have some variable that take care of whether my button and other widget should be shown or not, I already create a if statement for it and the issue is with detecting whether the map is currently empty or not.
Here's the code:
/// Initialize the data
Map data = {};
///If statement
if (povStatus == 'pic' && data.isEmpty) {
showReportEditButton = false;
showReportWriteButton = true;
showReportNotif = true;
showHeaderReport = false;
} else {
showReportEditButton = true;
showReportWriteButton = false;
showReportNotif = false;
showHeaderReport = true;
}
Based on the code above, when the data is empty it work just fine and enter the first if. But when I fill the "data" and the map becomes not empty it still enter the first if rather than the else part. What's the reason of this ?
You can read the entries:
Map<String,String> map={};
if(map.entries.isEmpty){
//Do something
}
What is the proper way to check if the list of elements is loading for the first time and if it has the exception to show the error to user?
As I understand I should create the variable which will indicate that I have the first list of item loading, so I wrote something like this:
bool isFirstLoading = true;
Future<void> loadItems()async{
if(!isFirstLoading){
final value = await model.loadListPlaceAgain();
}else if(isFirstLoading && Exception is true){
return _dialogController.showSnackBar(const PaginationBarError());
}
}
Like in Email, i need to show an option attachment it should show the option like browse or drag and drop , and the attached file should be less than 35MB. How to achieve in flutter.
To pick file use this plugin.
Code for picking file:-
FilePickerResult result = await FilePicker.platform.pickFiles();
if(result != null) {
PlatformFile file = result.files.first;
if(file.size>36700160)//if size greater than 35MB
{
result=null;
print('Size greater than 35MB');//or show the toast
}
} else {
// User canceled the picker
}
Now use this result to upload the file:-
RaisedButton(
child:Text("Upload")
onPressed:(){
if(result!=null)
uploadFile();//call your function for upload.
}
),
I'm using Flutter web for a webapp and having trouble converting an image from the image picker to a file in order to upload it to my server. I display the image in Image.file(xxx) but I get the error:
Error while trying to load an asset: FormatException: Illegal scheme
character (at character 6)
Image(image:%20MemoryImage(Uint8List%234267a,%20scale:%201),%20frameBuilder...
Here is the code I'm trying:
Future getImage(bool isCamera) async {
Image image;
if (isCamera) {
image = await FlutterWebImagePicker.getImage;
} else {
}
var bytes = await rootBundle.load('$image');
String tempPath = (await getTemporaryDirectory()).path;
File file = File('$tempPath/profile.png');
await file.writeAsBytes(
bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes));
setState(() {
currentSelfie = file;
_accDetails['customer_selfie'] = currentSelfie;
});
}
Thanks in advance
I've tested this package and was very happy with the result imagePickerWebit returns 3 different types it can be in the form of Image(widget for preview), byte, File(upload)
then you can use this to get the values
html.File _cloudFile;
var _fileBytes;
Image _imageWidget;
Future<void> getMultipleImageInfos() async {
var mediaData = await ImagePickerWeb.getImageInfo;
String mimeType = mime(Path.basename(mediaData.fileName));
html.File mediaFile =
new html.File(mediaData.data, mediaData.fileName, {'type': mimeType});
if (mediaFile != null) {
setState(() {
_cloudFile = mediaFile;
_fileBytes = mediaData.data;
_imageWidget = Image.memory(mediaData.data);
});
}
I havent used the plugin although your code has 2 issues. One is the if statement and the second one is using Rootbundle. If you are picking from the filesystem, my assumption isCamera would be false. You have not added any logic for the falsy condition.
if (isCamera) {// This would be true if the source was camera
image = await FlutterWebImagePicker.getImage;
} else {
}
Additionally,
var bytes = await rootBundle.load('$image');
From the flutter documentation, rootbundle contains the resources that were packaged with the application when it was built. Those are assets that you define in your pubspec. yaml. You are selecting an image at runtime hence its not bundled as an asset.
Since the package appears to return an image object, use the toByteData method on the image i.e
image = await FlutterWebImagePicker.getImage;
await image.toByteData();//This method has some parameters. Look into them
I am struggling with function that should check form fields before submitting.
I have some select (dropdown fields) and a text field. None of them should be empty for submit.
The script http://jsfiddle.net/6KY5J/2/ to reproduce.
I check dropdown fields within .each and additional text field. Here is the function:
function checkFields(e) {
$$('.dropdown').each(function (element) {
if (element.selectedIndex === 0) {
alert('Fill all dropdown fields!');
Event.stop(e);
throw $break;
return;
}
});
if ($('sometext').value == '') {
alert('Fill the input!');
Event.stop(e);
return;
}
alert('OK!');
}
But I am not able to prevent further execution of the script if one of dropdown is empty. Event.stop(e) seems to to work for the input field only in the second part.
Desired behaviour:
Check dropdowns, if one is empty, stop execution, do not make any
further checks.
Check text input field only if dropdowns are not empty.
Submit only if everything if filled.
The issue is in step 1. My script does not stop here, alerts, but does not stop. Any idea? Thank you!
function checkFields(e) {
var dropdownsokay = true;
$$('.dropdown').each(function (element) {
if (dropdownsokay && element.selectedIndex === 0) {
alert('Fill all dropdown fields!');
Event.stop(e);
dropdownsokay = false;
}
});
if(dropdownsokay) { //only check textfield if all the dropdowns are okay
if ($('sometext').value == '') {
alert('Fill the input!');
Event.stop(e);
return;
}
alert('OK!');
}
}