如何解决检查图像资产是否存在颤动
作为序言,我已经查看了帖子here,但仍然遇到麻烦。
仅当资产/图像/文件夹中存在该图像时,我才会构建一个装饰有图像的容器。如果它不存在,则将使用默认图像来装饰该容器。
图像路径是使用变量image_id格式化的字符串,该变量假定来自API的字符串响应。
这是我要执行的操作的代码/伪代码组合...
Container(
height: 200,width: 300,decoration: BoxDecoration(
image: DecorationImage(
image: (AssetImage('assets/images/${image_id}.jpg') exists) //pseudo-code here
? AssetImage('assets/images/${image_id}.jpg')
: AssetImage('assets/images/default.jpg'),fit: BoxFit.cover,),);
解决方法
您负责添加资产。这样,您就会知道它是否在那里。
,您应该知道资产是否存在,因为您是负责添加资产的人。
您可以使用以下一种方法:FlutterGen该库将为您的所有资产创建变量,方法等,如果变量为null,那么就可以了。
您也可以尝试获取如下图像:
function Login(props){ // receive props
// States needed to grab email and password
const [email,setEmail] = useState("");
const [password,setPassword] = useState("");
// Once form is submitted for login call this function
const onSubmit = async(event) => {
event.preventDefault();
try{
console.log("Submitted")
const user = await Auth.signIn(email,password);
// return <Redirect to='/about'/>
props.history.push('/about'); // use history
}catch(error){
console.log("Error")
console.log(error)
}
};
return (
<form onSubmit={onSubmit}>
<input value={email} onChange={event=>setEmail(event.target.value)} type="text" />
<input value={password} onChange={event=>setPassword(event.target.value)} type="text" />
<button>Sign Up</button>
</form>
);
}
,然后检查该图像是否为空。即使,如果您愿意,也可以这样做并预先缓存图像,如下所示:
final assetImage = Image.asset('path/to/asset.jpg');
,
您可以list所有资产。然后,您可以检查某些资产是否存在。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。