1

What I am trying to do: fetch data using google map api, then pass data to the next screen.

What's wrong: it works fine on iOS simulator, it manages to get to the next screen and populate the map with the data (prints position, "yay", http status code, and the "loopyays"). But on android emulator, it prints only position and "yay", and hangs there, does not navigate to the next screen.

What went wrong? :/ Please help me out..

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);
    }
  }

my config

  1. I have androidmapsdk activated on my google console
  2. in my androidmanifest I have this after the
< meta-data android:name="com.google.android.geo.API_KEY"
                android:value="AIzaSyDVzbjC2hViOnxxxxxxxxxx"/>
  1. in my build.gradle
    dependencies {
            classpath 'com.android.tools.build:gradle:3.3.1'
            classpath 'com.google.gms:google-services:4.2.0'
        }
  1. in my gradle.properties
    android.enableJetifier=true
    android.useAndroidX=true
    org.gradle.jvmargs=-Xmx1536M
Muldec
  • 4,641
  • 1
  • 25
  • 44
Julliard
  • 523
  • 1
  • 6
  • 23

1 Answers1

5

In Android 9 pie, http request is not permitted in default.

Solution: Please update this in your AndroidManifest.xml file like below.

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

        ... // Your current code.

    </application>

Have a good day.

Harsh Panchal
  • 826
  • 6
  • 16