我有一个应用程序,当用户注册时,个人资料照片URL
是必需的,尽管我注意到不是每个人都想在注册时添加个人资料照片。所以我希望,当用户不添加个人资料照片,然后一个默认的个人资料照片URL
被添加,而不是保存到Database
。下面是返回URL
的函数:
// add_a_file_to_aws_s3
Future<GetUrlResult> uploadImage(File? file, String? key) async {
try {
await Amplify.Storage.uploadFile(
local: file!,
key: key!,
onProgress: (progress) {
// ignore: avoid_print
print('Fraction completed: ${progress.getFractionCompleted()}');
},
);
GetUrlResult urlResult = await Amplify.Storage.getUrl(key: key);
return urlResult;
} on StorageException {
rethrow;
}
}
,下面是注册后将用户信息添加到Database
的代码:
final result = await Amplify.Auth.signUp(
username: username.text.trim(),
password: password.text.trim(),
options: CognitoSignUpOptions(userAttributes: userAttributes),
);
GetUrlResult urlResult = await uploadImage(file, 'Profile Photos');
await userController.addUserData(
username.text,
password.text,
emailAddress.text,
urlResult.toString(),
int.parse('zero'),
int.parse('zero'),
birthday.text,
gender.text,
location.text,
phoneNumber.text,
);
setState(() {
isSignUpComplete = result.isSignUpComplete;
});
我创建了userController();
文件,这就是我创建addUserData
函数的地方。现在我需要帮助添加默认的个人资料照片URL
到Database
,当用户不添加任何。谢谢你
Just do:
Future<GetUrlResult> uploadImage(File? file, String? key) async {
try {
await Amplify.Storage.uploadFile(
local: file!,
key: key!,
onProgress: (progress) {
// ignore: avoid_print
print('Fraction completed: ${progress.getFractionCompleted()}');
},
);
GetUrlResult urlResult = await Amplify.Storage.getUrl(key: key);
if(urlResult.isEmpty){ //just add this
default_url = 'your_photo_url';
return default_url;
} else {
return urlResult;
}
} on StorageException {
rethrow;
}
}