Android 模拟器不处理 http get 请求,但 iOS 模拟器工作正常

Jul*_*ard 1 android http dart flutter

我想要做的是:使用谷歌地图 api 获取数据,然后将数据传递到下一个屏幕。

有什么问题:它在 iOS 模拟器上运行良好,它设法进入下一个屏幕并用数据填充地图(打印位置、“yay”、http 状态代码和“loopyays”)。但是在 android 模拟器上,它只打印位置和“yay”,并挂在那里,不会导航到下一个屏幕。

什么地方出了错?:/ 请帮帮我..

class _LoadingScreenState extends State<LoadingScreen> {
  void getUserLocation() async {
    try {
      Position position = await Geolocator()
          .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
      print(position);
      Set<Marker> markers = {};
      String url =
          'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${position.latitude},${position.longitude}&maxprice=4&radius=1500&type=restaurant&keyword=cruise&key=AIzaSyDVzxxxxxxxxxxxxxxxxxxx';
      print('yay');
      http.Response data = await http.get(url);
      print(data.statusCode);
      var resultList = jsonDecode(data.body)['results'];
      for (Map i in resultList) {
        var coords = (i['geometry']['location']);
        print('loopyay');
        markers.add(Marker(
            markerId: MarkerId(i['name']),
            position: LatLng(coords['lat'], coords['lng'])));
      }

      Navigator.push(context, MaterialPageRoute(builder: (context) {
        return UserLocationScreen(
          userLocation: position,
          markers: markers,
        );
      }));
    } catch (e) {
      print(e);
    }
  }
Run Code Online (Sandbox Code Playgroud)

我的配置

  1. 我已androidmapsdk在我的 google 控制台上激活
  2. 在我的 androidmanifest 之后,我有这个
class _LoadingScreenState extends State<LoadingScreen> {
  void getUserLocation() async {
    try {
      Position position = await Geolocator()
          .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
      print(position);
      Set<Marker> markers = {};
      String url =
          'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${position.latitude},${position.longitude}&maxprice=4&radius=1500&type=restaurant&keyword=cruise&key=AIzaSyDVzxxxxxxxxxxxxxxxxxxx';
      print('yay');
      http.Response data = await http.get(url);
      print(data.statusCode);
      var resultList = jsonDecode(data.body)['results'];
      for (Map i in resultList) {
        var coords = (i['geometry']['location']);
        print('loopyay');
        markers.add(Marker(
            markerId: MarkerId(i['name']),
            position: LatLng(coords['lat'], coords['lng'])));
      }

      Navigator.push(context, MaterialPageRoute(builder: (context) {
        return UserLocationScreen(
          userLocation: position,
          markers: markers,
        );
      }));
    } catch (e) {
      print(e);
    }
  }
Run Code Online (Sandbox Code Playgroud)
  1. 在我的 build.gradle 中
    dependencies {
            classpath 'com.android.tools.build:gradle:3.3.1'
            classpath 'com.google.gms:google-services:4.2.0'
        }
Run Code Online (Sandbox Code Playgroud)
  1. 在我的 gradle.properties 中
    android.enableJetifier=true
    android.useAndroidX=true
    org.gradle.jvmargs=-Xmx1536M
Run Code Online (Sandbox Code Playgroud)

Har*_*hal 5

在 Android 9 pie 中,默认不允许 http 请求。

解决方案:请在您的AndroidManifest.xml文件中更新它,如下所示。

    <application
        ...
        android:usesCleartextTraffic="true">

        ... // Your current code.

    </application>
Run Code Online (Sandbox Code Playgroud)

祝你有美好的一天。