0

I am doing a flutter practice and I am making http requests, but I am having a problem and that is that when I make the request on the web it does it without problems but I run it on mobile and it tells me that the connection is refused.

import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:bombishi_mobile/models/auth_model.dart';

class AuthService {
  Future<void> login(AuthModel auth) async {
    final response = await http.post(
      Uri.parse('http://localhost:3000/api/v1/auth'),
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      },
      body: jsonEncode({
        'username': auth.username,
        'password': auth.password,
      }),
    );
  }
}
cosmos multi
  • 543
  • 1
  • 6
  • 13
  • 1
    Does this answer your question? [How to point to localhost:8000 with the Dart http package in Flutter?](https://stackoverflow.com/questions/47372568/how-to-point-to-localhost8000-with-the-dart-http-package-in-flutter) – Phil May 31 '23 at 01:22

1 Answers1

0

when you are using mobile, make sure to connect to the same wifi network and change your API to the computer network address like this

localhost -> 192.168.8.134 //your ip address

after that your api looks like this :-

http://192.168.8.134:3000/api/v1/auth
MrShakila
  • 874
  • 1
  • 4
  • 19