我是 Flutter 的新手,我正在嘗試添加 Google Map Places API 自動完成地點選擇器,
我用過:https ://pub.dev/packages/google_maps_place_picker
我按照他們的示例將代碼添加到我的主 dart 檔案中,例如:
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;
^
如何顯示地圖并選擇位置,問題是我認為當所選位置為null但我嘗試了很多方法,但它不起作用。我是新手,所以我沒有太多經驗。
uj5u.com熱心網友回復:
嘗試替換:Text(selectedPlace.formattedAddress ?? "")與Text(selectedPlace!.formattedAddress ?? "")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/427070.html
