Flutter app无法从Laravel后端API获取数据求助
Flutter调用Laravel接口返回空列表问题
我开发了一个基于Laravel后端的Flutter应用,已构建APK并安装在手机上,且手机与电脑处于同一WiFi网络。通过浏览器访问Laravel接口http://192.168.10.22:8000/api/polls可正常获取投票数据,Postman测试该接口也正常,但应用中调用该接口却返回空列表。
以下是我的代码:
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'); } }
我已尝试的解决方法:
- 在local.properties中设置
minSdkVersion=30以适配Android 11 - 创建规则允许通过8000端口访问Laravel应用
- 使用
php artisan serve --host=0.0.0.0 --port=8000将服务器绑定到0.0.0.0
内容的提问来源于stack exchange,提问作者Pham Xuan Nguyen
相关产品推荐
相关产品推荐

