ios – 500 Error: ‘_Map’ is not a subtype of type ‘String’


I’ve authentication feature in the iOS app (on flutter), here is the User class:

class User {
  final String fullname;
  final String username;
  final String email;
  final String password;
  final String token;
  final String type;
  final String id;
  final String pin;
  final bool isVerified;
  User({
    required this.fullname,
    required this.username,
    required this.email,
    required this.password,
    required this.token,
    required this.type,
    required this.id,
    required this.pin,
    required this.isVerified,
  });

  Map<String, dynamic> toMap() {
    return {
      'fullname': fullname,
      'username': username,
      'email': email,
      'password': password,
      'token': token,
      'type': type,
      'id': id,
      'pin': pin,
      'isVerified': isVerified,
    };
  }

  factory User.fromMap(Map<String, dynamic> map) {
    return User(
      fullname: map['fullname'] ?? '',
      username: map['username'] ?? '',
      email: map['email'] ?? '',
      password: map['password'] ?? '',
      token: map['token'] ?? '',
      type: map['type'] ?? '',
      id: map['_id'] ?? '',
      pin: map['pin'] ?? '',
      isVerified: map['isVerified'] ?? false,
    );
  }

  String toJson() => json.encode(toMap());

  factory User.fromJson(String source) => User.fromMap(json.decode(source));
}

The problem starts on login page after checking credentials:

void sendOtp({
   required BuildContext context,
   required String email,
   required String sendPurpose,
   required VoidCallback onTapDialogButton,
 }) async {
   try {
     showDialogLoader(context);

     http.Response res = await http
         .post(Uri.parse("$uri/api/sendOtp/$sendPurpose"),
             headers: <String, String>{
               "Content-Type": "application/json; charset=UTF-8",
             },
             body: jsonEncode({
               "email": email,
             }))
         .timeout(const Duration(seconds: 25));
     Navigator.of(context, rootNavigator: true).pop('dialog');
     print(res.statusCode);
     statusCodeHandler(
       context: context,
       response: res,
       onSuccess: () {
         showAlertMessage(
           context: context,
           title: "Verify Account",
           message: "Please Check Mail For OTP Code",
           onTap: onTapDialogButton,
         );
       },
     );
   } on TimeoutException catch (e) {
     showTimeOutError(
       context: context,
       popDialogAndLoader: true,
     );
   } on SocketException catch (e) {
     showNoInternetError(
       context: context,
       popDialogAndLoader: true,
     );
   } on Error catch (e) {
     print('Send Otp Error: $e');
   }
 }

the error on the Console says Send Otp Error: type '_Map<String, dynamic>' is not a subtype of type 'String' and the main page that should appear after authentication doesn’t open

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img