I have tried to create a PlacePicker widget with the help of chatgpt and now i get no defined errors but still some undefined ones, but i dont understand what. Any help i greatly appreciated😀
Here is the code:// Automatic FlutterFlow imports import '/backend/backend.dart'; import '/flutter_flow/flutter_flow_theme.dart'; import '/flutter_flow/flutter_flow_util.dart'; import '/custom_code/widgets/index.dart'; // Imports other custom widgets import '/custom_code/actions/index.dart'; // Imports custom actions import '/flutter_flow/custom_functions.dart'; // Imports custom functions import 'package:flutter/material.dart'; // Begin custom widget code // DO NOT REMOVE OR MODIFY THE CODE ABOVE! import 'package:google_maps_flutter/google_maps_flutter.dart' as maps; import 'package:google_maps_webservice/places.dart' hide LatLng; import 'package:flutter_google_places/flutter_google_places.dart'; // Add the necessary imports for FFAppState and LatLng // import 'path_to/ffapp_state.dart'; // Update this with the actual path // import 'package:latlong/latlong.dart'; // This package may vary based on your implementation class PlacePicker extends StatefulWidget { const PlacePicker({ Key? key, this.width, this.height, required this.onPlacePicked, required this.action, // Initialized here }) : super(key: key); final double? width; final double? height; final Function(PlacesDetailsResponse) onPlacePicked; final Future<dynamic> Function() action; // Declare the action @override _PlacePickerState createState() => _PlacePickerState(); } class _PlacePickerState extends State<PlacePicker> { maps.GoogleMapController? _mapController; maps.LatLng _center = const maps.LatLng(45.521563, -122.677433); maps.LatLng? _lastMapPosition; late GoogleMapsPlaces _places; bool _isApiKeyReady = false; // Flag for API key readiness @override void initState() { super.initState(); _lastMapPosition = _center; _initializeApiKey(); } Future<void> _initializeApiKey() async { try { String apiKey = await _googleMapsApiKey(); setState(() { _places = GoogleMapsPlaces(apiKey: apiKey); _isApiKeyReady = true; }); } catch (e) { // Handle the error print('Error initializing API key: $e'); } } Future<String> _googleMapsApiKey() async { // Implement your API key retrieval logic here return 'YOUR_API_KEY'; } void _onMapCreated(maps.GoogleMapController controller) { _mapController = controller; } void _onCameraMove(maps.CameraPosition position) { _lastMapPosition = position.target; } void _onSearchPressed() async { if (!_isApiKeyReady) return; try { Prediction? p = await PlacesAutocomplete.show( context: context, apiKey: await _googleMapsApiKey(), mode: Mode.overlay, language: "en", components: [Component(Component.country, "us")], ); if (p != null) { PlacesDetailsResponse detail = await _places.getDetailsByPlaceId(p.placeId!); final lat = detail.result.geometry?.location.lat; final lng = detail.result.geometry?.location.lng; if (lat != null && lng != null) { _mapController?.animateCamera( maps.CameraUpdate.newCameraPosition( maps.CameraPosition(target: maps.LatLng(lat, lng), zoom: 12.0), ), ); widget.onPlacePicked(detail); // Assign the place's formatted address if it's not null; otherwise, use a default value String address = detail.result.formattedAddress ?? 'Default Address'; FFAppState().address = address; // Update the location in FFAppState using LatLng from latlong2 package FFAppState().locationLatLng = LatLng(lat, lng); // Call the action if it's provided widget.action.call(); // Update the UI after the state has changed setState(() {}); } } } catch (e) { print("Error occurred while fetching place details: $e"); } } @override Widget build(BuildContext context) { return SizedBox( width: widget.width ?? MediaQuery.of(context).size.width, height: widget.height ?? MediaQuery.of(context).size.height, child: Scaffold( appBar: AppBar( title: const Text('Place Picker'), actions: <Widget>[ if (_isApiKeyReady) IconButton( icon: const Icon(Icons.search), onPressed: _onSearchPressed, ), ], ), body: maps.GoogleMap( onMapCreated: _onMapCreated, initialCameraPosition: maps.CameraPosition( target: _center, zoom: 11.0, ), onCameraMove: _onCameraMove, ), floatingActionButton: _isApiKeyReady ? FloatingActionButton.extended( onPressed: () { // Implement what happens when the button is pressed }, label: const Text('Select Here'), icon: const Icon(Icons.map), ) : null, ), ); } }
And her are the paramaters: