Home > database >  Why Error: Property 'formattedAddress' cannot be accessed on 'PickResult?' becau
Why Error: Property 'formattedAddress' cannot be accessed on 'PickResult?' becau

Time:02-06

I'm new to Flutter, I'm trying to add Google Map Places API Auto Complete places picker,

I used : https://pub.dev/packages/google_maps_place_picker

I followed their example and added the code into my main dart file like :

import 'package:flutter/material.dart';
import 'package:google_maps_place_picker/google_maps_place_picker.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // Light Theme
  final ThemeData lightTheme = ThemeData.light().copyWith(
    // Background color of the FloatingCard
    cardColor: Colors.white,
    buttonTheme: ButtonThemeData(
      // Select here's button color
      buttonColor: Colors.black,
      textTheme: ButtonTextTheme.primary,
    ),
  );

  // Dark Theme
  final ThemeData darkTheme = ThemeData.dark().copyWith(
    // Background color of the FloatingCard
    cardColor: Colors.grey,
    buttonTheme: ButtonThemeData(
      // Select here's button color
      buttonColor: Colors.yellow,
      textTheme: ButtonTextTheme.primary,
    ),
  );

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Google Map Place Picker Demo',
      theme: lightTheme,
      darkTheme: darkTheme,
      themeMode: ThemeMode.light,
      home: HomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class HomePage extends StatefulWidget {


  static final kInitialPosition = LatLng(-33.8567844, 151.213108);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  PickResult? selectedPlace;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Google Map Place Picer Demo"),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              RaisedButton(
                child: Text("Load Google Map"),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) {
                        return PlacePicker(
                          apiKey: 'API-KEY',
                          initialPosition: HomePage.kInitialPosition,
                          useCurrentLocation: true,
                          selectInitialPosition: true,

                          //usePlaceDetailSearch: true,
                          onPlacePicked: (result) {
                            selectedPlace = result;
                            Navigator.of(context).pop();
                            setState(() {});
                          },
                          
                        );
                      },
                    ),
                  );
                },
              ),
              selectedPlace == null
                  ? Container()
                  : Text(selectedPlace.formattedAddress ?? ""),
            ],
          ),
        ));
  }
}



But when I try to run the code I get error like :

Xcode's output:
↳
    lib/main.dart:92:40: Error: Property 'formattedAddress' cannot be accessed on 'PickResult?' because it is potentially null.
     - 'PickResult' is from 'package:google_maps_place_picker/src/models/pick_result.dart' ('../../Developer/flutter/.pub-cache/hosted/pub.dartlang.org/google_maps_place_picker-2.1.0-nullsafety.3/lib/src/models/pick_result.dart').
    Try accessing using ?. instead.
                      : Text(selectedPlace.formattedAddress ?? ""),
                                           ^^^^^^^^^^^^^^^^
    lib/main.dart:52:15: Context: 'selectedPlace' refers to a property so it couldn't be promoted.
    See http://dart.dev/go/non-promo-property
      PickResult? selectedPlace;
                  ^

How can show the map and select the location, The problem is i think when the selected location is null but i tried many ways but it doesnt work. Im new to flutter so i dont have much experience.

CodePudding user response:

try replacing: Text(selectedPlace.formattedAddress ?? "") with Text(selectedPlace!.formattedAddress ?? "")

  •  Tags:  
  • Related