'How to move google map view to address typed in Flutter?

Currently, I create a map application in android. The user will type the address in the text field and then the map will show the location. The application always forced close, and I do not know the problem, any help would be appreciated.

Here is my console result:

E/AndroidRuntime(26283): FATAL EXCEPTION: AsyncTask #2 E/AndroidRuntime(26283): Process: com.example.service_platform, PID: 26283 E/AndroidRuntime(26283): java.lang.IllegalArgumentException: locationName == null E/AndroidRuntime(26283): at android.location.Geocoder.getFromLocationName(Geocoder.java:171) E/AndroidRuntime(26283): at com.baseflow.geolocator.tasks.ForwardGeocodingTask$1.run(ForwardGeocodingTask.java:42) E/AndroidRuntime(26283): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) E/AndroidRuntime(26283): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) E/AndroidRuntime(26283): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) E/AndroidRuntime(26283): at java.lang.Thread.run(Thread.java:764) I/Process (26283): Sending signal. PID: 26283 SIG: 9

Here is my code for converting searchAddress:

searchandNavigate(){
Geolocator().placemarkFromAddress(searchAddr).then((result) => {
  mapController.animateCamera(CameraUpdate.newCameraPosition(
      CameraPosition(target: LatLng(result[0].position.latitude,result[0].position.longitude), zoom: 15)
  ))
});
}

And here is the full code:

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:service_platform/common/helper/size_config.dart';

class ProfileMap extends StatefulWidget {
  @override
  _ProfileMapState createState() => _ProfileMapState();
}

class _ProfileMapState extends State<ProfileMap> {
  LatLng point = LatLng(-6.200000, 106.816666);
  
  Set<Marker> _marker={};

  String searchAddr;

  GoogleMapController mapController;

  searchandNavigate(){
    Geolocator().placemarkFromAddress(searchAddr).then((result) => {
      mapController.animateCamera(CameraUpdate.newCameraPosition(
          CameraPosition(target: LatLng(result[0].position.latitude,result[0].position.longitude), zoom: 15)
      ))
    });
  }

  void onMapCreated(GoogleMapController controller){
    mapController = controller;
  }
  void _onMapTap(p){
    setState(() {
      point = p;
      _marker.clear();
      _marker.add(Marker(
        markerId: MarkerId('id_0'),
        position: point,
        draggable: true,
      ));
    });
  }
@override
  Widget build(BuildContext context) {
    SizeConfig().init(context);
    var size = MediaQuery.of(context).size;

    return Scaffold(
      resizeToAvoidBottomInset: true,
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text('Place Pin Point', style: TextStyle(color: Color(0xff222222))),
        centerTitle: true,
        titleTextStyle: TextStyle(color: Color(0xff222222), fontWeight: FontWeight.w700, fontSize: 16),
        iconTheme: IconThemeData(
            color: Color(0xffee3124)
        ),
      ),
      body: Stack(
        alignment: Alignment.bottomCenter,
          children: [
            GoogleMap(
              onMapCreated: onMapCreated,
              initialCameraPosition: CameraPosition(
                  target: LatLng(22.22, 88.88),
                  zoom: 15
              ),
              markers: _marker,
              onTap: _onMapTap,
              zoomControlsEnabled: false,
            ),
            Padding(
              padding: const EdgeInsets.all(16),
              child: Container(
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(4)
                      ),
                      child: Card(
                        child: TextFormField(
                          onChanged: (val){
                            setState(() {
                              searchAddr = val;
                            });
                          },
                          onEditingComplete: searchandNavigate,
                        ),
                      ),
                    ),
               
            )
          ]
        ),
    );
  }
}

Thanks in advance



Solution 1:[1]

Ok, I found the error, it's because the address is still null when the program processes it. Just adding If(address!="") solves all problems.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 My Car