Why Error: Property 'formattedAddress' cannot be accessed on 'PickResult?' because it is potentially null?
Why Error: Property 'formattedAddress' cannot be accessed on 'PickResult?' because it is potentially null?
我是 Flutter 的新手,我正在尝试添加 Google 地图地点 API 自动完成地点选择器,
我用过:https://pub.dev/packages/google_maps_place_picker
我按照他们的例子将代码添加到我的主飞镖文件中,如:
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 ?? ""),
],
),
));
}
}
但是当我尝试 运行 代码时,我得到如下错误:
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;
^
如何显示地图和 select 位置,问题是我认为当 selected 位置为 null 但我尝试了很多方法但它不起作用。
我是 flutter 的新手,所以我没有太多经验。
尝试用 Text(selectedPlace!.formattedAddress ?? "")
替换:Text(selectedPlace.formattedAddress ?? "")
我是 Flutter 的新手,我正在尝试添加 Google 地图地点 API 自动完成地点选择器,
我用过:https://pub.dev/packages/google_maps_place_picker
我按照他们的例子将代码添加到我的主飞镖文件中,如:
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 ?? ""),
],
),
));
}
}
但是当我尝试 运行 代码时,我得到如下错误:
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;
^
如何显示地图和 select 位置,问题是我认为当 selected 位置为 null 但我尝试了很多方法但它不起作用。 我是 flutter 的新手,所以我没有太多经验。
尝试用 Text(selectedPlace!.formattedAddress ?? "")
Text(selectedPlace.formattedAddress ?? "")