skip to Main Content

auth_service.dart
The following ArgumentError was thrown resolving an I/flutter (11774): Invalid argument(s): No host specified in URI
This my Code pleaseeeeeeeee help meeee

class AuthService {
  String baseUrl = 'http://shamo-backend.buildwithangga.id/api';

  Future<UserModel?> register({
    required String name,
    required String username,
    required String email,
    required String password,
  }) async {
    // ignore: unused_local_variable
    var url = '$baseUrl/register';
    var header = {'Content-Type': 'application/json'};
    var body = jsonEncode({
      'name': name,
      'username': username,
      'email': email,
      'password': password,
    });

    var response = await http.post(
      Uri(),
      headers: header,
      body: body,
    );

    print(response.body);

    if (response.statusCode == 200) {
      var data = jsonDecode(response.body)['data'];
      UserModel user = UserModel.fromJson(data['user']);
      // ignore: prefer_interpolation_to_compose_strings
      user.token = 'Bearer ' + data['access_token'];

      return user;
    } else {
      throw Exception('Gagal Register');
    }
  }
}

Iwant save like this enter image description here
Please Helpme

2

Answers


  1. You are not passing any url in http.post. Replace

        var response = await http.post(
          Uri(),
          headers: header,
          body: body,
        );
    

    With

        var response = await http.post(
          Uri.parse(url),
          headers: header,
          body: body,
        );
    

    Refer this documentation for detail information

    Login or Signup to reply.
  2. Use it like this to resove this issue

    // edit this line of your code
    ====> var header = {'Content-Type': 'application/json'};
    
    // with this
    ====> var header = {'Content-Type': 'application/json; charset=UTF-8'};
    
    
    // FULL CODE
    class CreateGoalModals {
        Future<Add_Goal_Status> create_goal_WB(
        String name,
        String username,
        String email,
       String password,
    
    ) async {
    
    final response = await http.post(
      Uri.parse(
        your_application_base_url,
      ),
      headers: <String, String>{
        'Content-Type': 'application/json; charset=UTF-8',
      },
    
      body: jsonEncode(
        <String, String>{
          'name': name,
          'username': username,
          'email': email,
          'password': password,
        },
      ),
    );
    
    
    if (response.statusCode == 201) {
    print('=========> 201');
      print(response.body);
    
      
    } else if (response.statusCode == 200) {
      print('==========> 200');
      print(response.body);
    
      
    
      // after SUCCESS
      if (success_text == "success") {
        print('=========>  SUCCESSFULLY <===========');
    
      } else {
        print('========> SUCCESS WORD FROM SERVER IS WRONG <=========');
        
      }
      // throw Exception('SOMETHING WENT WRONG. PLEASE CHECK');
    } else {
      print("============> ERROR");
      print(response.body);
    
      
    }
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search