参数类型 'File?' 无法分配给参数类型 'Future<File>?'
The argument type 'File?' can't be assigned to the parameter type 'Future<File>?'
File ? file;
String status = '';
String ? base64Image;
File ? tmpFile;
String errMessage = 'Error Uploading Image';
chooseImage() async {
var image = await ImagePicker().pickImage(source: ImageSource.gallery);
setState(() {
file = File(image!.path);
});
setStatus('');
print(base64Image);
}
startUpload() {
setStatus('Uploading Image...');
if (null == tmpFile) {
setStatus(errMessage);
return;
}
String fileName = tmpFile!.path.split('/').last;
upload(fileName);
}
setStatus(String message) {
setState(() {
status = message;
});
}
upload(String fileName) {
http.post(Uri.parse(URL), body: {
"image": base64Image,
"name": fileName,
}).then((result) {
setStatus(result.statusCode == 200 ? result.body : errMessage);
}).catchError((error) {
setStatus(error);
});
}
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: Image.file(
snapshot.data!,
fit: BoxFit.fill,
),
);
} else if (null != snapshot.error) {
return const Text(
'Error Picking Image',
textAlign: TextAlign.center,
);
} else {
return const Text(
'No Image Selected',
textAlign: TextAlign.center,
);
}
},
);
}
我正在尝试选择一张图片并将其转换为 base64
您不需要使用 FutureBuilder 来使用 File
而是直接使用它:
Widget showImage() {
if (file != null) {
tmpFile = file!;
base64Image = base64Encode(file!.readAsBytesSync());
return Flexible(
child: Image.file(
file!,
fit: BoxFit.fill,
),
);
} else {
return const Text(
'No Image Selected',
textAlign: TextAlign.center,
);
}
}```
File ? file;
String status = '';
String ? base64Image;
File ? tmpFile;
String errMessage = 'Error Uploading Image';
chooseImage() async {
var image = await ImagePicker().pickImage(source: ImageSource.gallery);
setState(() {
file = File(image!.path);
});
setStatus('');
print(base64Image);
}
startUpload() {
setStatus('Uploading Image...');
if (null == tmpFile) {
setStatus(errMessage);
return;
}
String fileName = tmpFile!.path.split('/').last;
upload(fileName);
}
setStatus(String message) {
setState(() {
status = message;
});
}
upload(String fileName) {
http.post(Uri.parse(URL), body: {
"image": base64Image,
"name": fileName,
}).then((result) {
setStatus(result.statusCode == 200 ? result.body : errMessage);
}).catchError((error) {
setStatus(error);
});
}
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: Image.file(
snapshot.data!,
fit: BoxFit.fill,
),
);
} else if (null != snapshot.error) {
return const Text(
'Error Picking Image',
textAlign: TextAlign.center,
);
} else {
return const Text(
'No Image Selected',
textAlign: TextAlign.center,
);
}
},
);
}
我正在尝试选择一张图片并将其转换为 base64
您不需要使用 FutureBuilder 来使用 File
而是直接使用它:
Widget showImage() {
if (file != null) {
tmpFile = file!;
base64Image = base64Encode(file!.readAsBytesSync());
return Flexible(
child: Image.file(
file!,
fit: BoxFit.fill,
),
);
} else {
return const Text(
'No Image Selected',
textAlign: TextAlign.center,
);
}
}```