2
import 'package:http/http.dart' show Client;
import 'dart:convert';
import 'package:todoapp/models/classes/user.dart';

class ApiProvider {
  Client client = Client();
  final _apiKey = 'api key here';

  Future<User> registerUser(String username, String firstname, String lastname,
      String password, String email) async {
    print("Hello");
    final response = await client.post("http://10.0.2.2:8000/api/register",
        body: jsonEncode({
          "email": email,
          "username": username,
          "password": password,
          "firstname": firstname,
          "lastname": lastname
        }));
    final Map result = json.decode(response.body);
    print(result["data"].toString());
    if (response.statusCode == 200) {
      return User.fromJson(result["data"]);
    } else {
      throw Exception('Failed to load post');
    }
  }
}

Hey, I am pretty much stuck here , cause I am getting an error which I tried a lot but couldn't fix it.

final response = await client.post("http://10.0.2.2:8000/api/register",
        body: jsonEncode({
          "email": email,
          "username": username,
          "password": password,
          "firstname": firstname,
          "lastname": lastname
        }));

The exception occurring is on the above line.

I am using REST API for the backend here.

0 Answers0