Can anyone show me how to get and post data with API in flutter? I have tried some flutter API tutorials with Laravel but when I print the value is error cors, something wrong with my flutter code or my API? or there are some settings in laravel when i create API?
create CRUD in Flutter using API from laravel
CodePudding user response:
I think You are having CORS issue from your mobile app. From the backend (laravel) you need to allow the request from your specific IP or even you can allow for all the domains. Please refer to this link on how to enable CORS on laravel. https://www.stackhawk.com/blog/laravel-cors/
CodePudding user response:
Future<List<dynamic>> _fecthDataUsers() async {
var result = await http.get(
Uri.parse(apiUrl),
headers: {
HttpHeaders.authorizationHeader:
'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvcG9zLmthcnlhb3B0aW1hLmNvbVwvYXBpXC9hdXRoXC9sb2dpbiIsImlhdCI6MTY2OTM4NzQ5MywibmJmIjoxNjY5Mzg3NDkzLCJqdGkiOiJpaWhvTzFTTEFKRWNxQTRHIiwic3ViIjoiM2I4OGI5NGMtY2VhZi00ODEyLWEwYmYtNzdiZDkzYWZlYmNjIiwicHJ2IjoiMjNiZDVjODk0OWY2MDBhZGIzOWU3MDFjNDAwODcyZGI3YTU5NzZmNyJ9.7x8Jgy6IqyP6MqKyGjZ7TgadoSQzj9lGyZbMXw6razw',
'Content-Type': 'application/json',
},
);
print(result.body);
return json.decode(result.body)['data'];