I created an flutter app with a Laravel back-end. I build the APK file for my app and installed it on to my mobile phone and connected my mobile phone to the same network as my computer with WiFi (my WiFi adapter is the same network as my computer).
When I try to use my web browser to connect my Laravel app (http://192.168.10.22:8000/api/polls), it shows expected results (for example, the poll titles). But when I try to use my app, I get empty list. Here is my code:
Future<void> fetchPolls() async {
print('Fetching polls...');
final response =
await http.get(Uri.parse('http://192.168.10.22:8000/api/polls'));
if (response.statusCode == 200) {
print('Response body: ${response.body}'); // Log the response body
setState(() {
polls = jsonDecode(response.body);
print('Polls: $polls'); // Log the polls list
});
} else {
print(
'Failed to load polls: ${response.statusCode}'); // Log the error status
throw Exception('Failed to load polls');
}
}
I tried the same using Postman, http://192.168.10.22:8000/api/polls – It is ok.
Here are the things I’ve tried:
- Added
local.properties : minSdkVersion=30
to make my app compatible with android 11 - Created rule to access Laravel app via 8000 port.
php artisan serve --host=0.0.0.0 --port=8000
to bind the server to0.0.0.0
2
Answers
Check
You must have Internet permission to be able to connect to Internet and get data from your APIs.
something like this:
After that don’t forget to do the following steps:
HTTP without SSL (HTTPS) are disabled by default in Flutter.
Just add usesCleartextTraffic flagfor debug builds in AndroidManifest.xml
Follow this link for more info: https://docs.flutter.dev/release/breaking-changes/network-policy-ios-android