flutter UserPostUpdate事件期间UserRepositoryImpl缺少“update”方法的问题

y1aodyip  于 2023-06-30  发布在  Flutter
关注(0)|答案(1)|浏览(107)

我目前正在做一个Flutter项目,我正在实现用户配置文件更新。但是,我遇到了UserRepositoryImpl类的问题,它似乎缺少处理UserPostUpdate事件所需的更新方法。我在下面提供了相关的代码片段以供参考。

// Code snippet 1: user_bloc and user_event

if (event is UserPostUpdate) {
  emit(UserPostLoading());
  try {
    await Future.delayed(const Duration(seconds: 2), () async {
      user = await userRepository.update(
        name: event.name,
        email: event.email,
        phoneNumber: event.phoneNumber,
        birthdate: event.birthdate,
        location: event.location,
        gender: event.gender,
        jobStatus: event.jobStatus,
        bio: event.bio,
        skills: event.skills,
      );

      emit(UserPostSuccess());
    });
  } catch (error) {
    emit(UserPostError(error.toString()));
  }
}

class UserPostUpdate extends UserEvent {
  // Event properties...

  UserPostUpdate(
    this.name, 
    this.email, 
    this.phoneNumber,
    this.birthdate, 
    this.location, 
    this.gender, 
    this.jobStatus,
    this.bio,
    this.skills,
  );
}
//code snippet 2: userRepository
class UserRepositoryImpl implements UserRepository {
  // Other repository methods...

  Future<UserModel> update({
    String? name,
    String? email,
    String? phoneNumber,
    String? birthdate,
    String? location,
    String? gender,
    String? jobStatus,
    String? bio,
    List<String>? skills,
  }) async {
    String token = await getToken();
    print('start update');

    // Buat Map untuk menyimpan field-field yang diupdate
    Map<String, dynamic> requestBody = {};

    if (name != null) {
      requestBody["name"] = name;
    }
    if (email != null) {
      requestBody["email"] = email;
    }
    if (phoneNumber != null) {
      requestBody["phone_number"] = phoneNumber;
    }
    if (birthdate != null) {
      requestBody["birthdate"] = birthdate;
    }
    if (location != null) {
      requestBody["location"] = location;
    }
    if (gender != null) {
      requestBody["gender"] = gender;
    }
    if (jobStatus != null) {
      requestBody["job_status"] = jobStatus;
    }
    if (bio != null) {
      requestBody["bio"] = bio;
    }
    if (skills != null) {
      String skillsString = skills.join(",");
      requestBody["skills"] = skillsString;
    }

    final response = await http.post(Uri.parse('$baseUrl/users/update'),
        body: requestBody, headers: {"Authorization": token});
}
// Code snippet 3: EditProfilePage widget
class EditProfilePage extends StatefulWidget {
  const EditProfilePage();

  @override
  _EditProfilePageState createState() => _EditProfilePageState();
}

class _EditProfilePageState extends State<EditProfilePage> {
  String message = "null";
  bool isChecked = false;
  String? selectedDate;
  List<UserModel>? user;
  List<String> userSkill = [];
  Widget build(BuildContext context) {
    UserModel loggedInUser =
        ModalRoute.of(context)!.settings.arguments as UserModel;

    TextEditingController nameController = TextEditingController(text: loggedInUser.name);
    TextEditingController genderController = TextEditingController(text: loggedInUser.gender);
    TextEditingController jobStatusController = TextEditingController(text: loggedInUser.jobStatus);
    TextEditingController bioController = TextEditingController(text: loggedInUser.bio);
    TextEditingController locationController = TextEditingController(text: loggedInUser.location);
    TextEditingController phoneNumberController =
        TextEditingController(text: '');
    TextEditingController emailController = TextEditingController(text: '');
    TextEditingController passwordController = TextEditingController(text: '');
    // Access the properties of the logged-in user
    String? username = loggedInUser.username;
    String? email = loggedInUser.email;
    String? profilePhotoPath = loggedInUser.profilePhotoPath;

    handleUpdate(String message) {
      print("userSkill edit: $userSkill");
      print("message: $message");
      if (message != "null") {
        ScaffoldMessenger.of(context).showSnackBar(CustomSnackbar(
          color: Colors.red,
          icon: Icons.warning,
          message: message,
        ));
      }

      BlocProvider.of<UserBloc>(context).add(UserPostUpdate(
          nameController.text,
          emailController.text,
          phoneNumberController.text,
          selectedDate!,
          locationController.text,
          genderController.text,
          jobStatusController.text,
          bioController.text,
          userSkill));
      setState(() {
        message = "null";
      });
      print(message);
    }

问题

我面临的问题是,当UserPostUpdate事件被触发时,它试图用所需的参数调用UserRepositoryImpl类的update方法。但是,我收到一个NoSuchMethodError,指出类没有带有匹配参数的示例方法“update”。我不确定为什么会出现这个错误,因为这个方法似乎在仓库类中定义正确。

错误

NoSuchMethodError: Class 'UserRepositoryImpl' has no instance method 'update' with matching arguments.
Receiver: Instance of 'UserRepositoryImpl'
Tried calling: update("john", "", "", "2003-6-30", "brazil", "male", "Student", "", Instance(length:5) of '_GrowableList')
Found: update({String? name, String? email, String? phoneNumber, String? birthdate, String? location, String? gender, String? jobStatus, String? bio, List<String>? skills}) => Future<UserModel>

我希望了解为什么UserRepositoryImpl类的update方法在UserPostUpdate事件期间无法识别。如果有任何替代方法或潜在的解决方案,请让我知道。提前感谢您的帮助!

nhjlsmyf

nhjlsmyf1#

错误抛出我认为它找不到任何匹配更新函数的参数
可能是Instance(length:5) of '_GrowableList'
你有没有试过把空列表,看看它是否通过或不?或者在您的存储库中有任何其他方法名称更新吗?
参考:https://API.flutter.dev/flutter/dart-core/NoSuchMethodError-class.html#:~:text= Error%20thrown%20on%20an%20invalid,checking%20prevents%20such%20invalid%20arguments.

相关问题