我想使用函数更新图片,但它总是说 Future<dynamic> 不是 ImageProvider<dynamic> 类型的子类型
I want to update picture using function but it always says Future<dynamic> is not a sub type of type ImageProvider<dynamic>
我想使用函数更新图片,但它总是说 Future 不是 ImageProvider 类型的子类型。
函数跟随小部件中的图像提供程序调用的函数 tree.It 在使用从图像中挑选的 ImageFile 时完美工作 provider.But 我将该文件复制到磁盘并检索它没有没用。
_decideImageView(){
if(imageFile== null){
return AssetImage("assets/example.jpeg");
}else{
return AssetImage('$_localPath/name.jpg');
}
工作一个
_decideImageView(){
if(imageFile== null){
return AssetImage("assets/example.jpeg");
}else{
return FileImage(imageFile);
}
}
通过此功能选择图片。
_openGallery(BuildContext context) async{
var picture = await ImagePicker.pickImage(source: ImageSource.gallery);
this.setState((){
imageFile = picture;
});
Directory pathd = await getApplicationDocumentsDirectory();
String path =pathd.path;
final File newImage = await imageFile.copy('$path/name.jpg');
Navigator.of(context).pop();
}
我已经使用
向 main.dart 提供了应用程序目录
static final _localPath = getApplicationDocumentsDirectory();
我要指出的是documentation for getApplicationDocumentsDirectory。如果您查看它,您会发现它 returns 一个 Future<Directory>
。该应用程序无法知道您是否在 运行 同步时解决了未来,因此它会抛出错误。
在调用它们之前解决期货的解决方案是使用 initState
像这样:
@override
void initState() {
super.initState();
getApplicationDocumentsDirectory().then((directory) {
_localPath = directory;
});
}
我建议您阅读异步编程,因为它很有趣。我希望这对你有帮助,你可以进一步开发你的应用程序!
我想使用函数更新图片,但它总是说 Future 不是 ImageProvider 类型的子类型。
函数跟随小部件中的图像提供程序调用的函数 tree.It 在使用从图像中挑选的 ImageFile 时完美工作 provider.But 我将该文件复制到磁盘并检索它没有没用。
_decideImageView(){
if(imageFile== null){
return AssetImage("assets/example.jpeg");
}else{
return AssetImage('$_localPath/name.jpg');
}
工作一个
_decideImageView(){
if(imageFile== null){
return AssetImage("assets/example.jpeg");
}else{
return FileImage(imageFile);
}
}
通过此功能选择图片。
_openGallery(BuildContext context) async{
var picture = await ImagePicker.pickImage(source: ImageSource.gallery);
this.setState((){
imageFile = picture;
});
Directory pathd = await getApplicationDocumentsDirectory();
String path =pathd.path;
final File newImage = await imageFile.copy('$path/name.jpg');
Navigator.of(context).pop();
}
我已经使用
向 main.dart 提供了应用程序目录static final _localPath = getApplicationDocumentsDirectory();
我要指出的是documentation for getApplicationDocumentsDirectory。如果您查看它,您会发现它 returns 一个 Future<Directory>
。该应用程序无法知道您是否在 运行 同步时解决了未来,因此它会抛出错误。
在调用它们之前解决期货的解决方案是使用 initState
像这样:
@override
void initState() {
super.initState();
getApplicationDocumentsDirectory().then((directory) {
_localPath = directory;
});
}
我建议您阅读异步编程,因为它很有趣。我希望这对你有帮助,你可以进一步开发你的应用程序!