dart 默认个人资料照片

cld4siwp  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(144)

我有一个应用程序,当用户注册时,个人资料照片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函数。现在我需要帮助添加默认的个人资料照片URLDatabase时,用户没有添加任何。谢谢

tpxzln5u

tpxzln5u1#

只是做:

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;
    }
  }

相关问题