0

How can I connect my flutter apps with laravel api? Below is what I post in postman.

http://10.0.2.2/voyce/api/register

I try to run the API in postman and it does not give any response.

here is my api.dart in flutter.


api.dart

import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
class CallApi{
    final String _url = 'http://10.0.2.2/voyce/api/';

    postData(data, apiUrl) async {
        var fullUrl = _url + apiUrl + await _getToken(); 
        return await http.post(
            fullUrl, 
            body: jsonEncode(data), 
            headers: _setHeaders()
        );
    }
    getData(apiUrl) async {
       var fullUrl = _url + apiUrl + await _getToken(); 
       return await http.get(
         fullUrl, 
         headers: _setHeaders()
       );
    }

    _setHeaders() => {
        'Content-type' : 'application/json',
        'Accept' : 'application/json',
    };

    _getToken() async {
        SharedPreferences localStorage = await
        SharedPreferences.getInstance();
        var token = localStorage.getString('token');
        return '?token=$token';
    }
}

All I want it to make the api able to connect? I think maybe there is something wrong with my laravel API but I dont have any idea where to fix it.

bihanns
  • 49
  • 1
  • 2
  • 10

1 Answers1

0

If you've encounter the error SocketException: OS Error: Connection refused, this is most likely a network connection error. When trying to post the url in the browser and you get the proper response, then the problem could be in the postman. But you can refer here for more details on how to integrate Laravel in Flutter.

MαπμQμαπkγVπ.0
  • 5,887
  • 1
  • 27
  • 65