属性 'latitude' 不能无条件访问,因为接收者可以是 'null'。尝试使访问有条件(使用“?”)
The property 'latitude' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.')
我在 Text(
"纬度:" + currentPosition.latitude.toString(),
) 这一行。我不知道如何让 currentPosition.latitude not null 帮助我解决这些问题。
它给我这个 属性 'latitude' 不能无条件访问因为接收者可以是 'null'。
尝试使访问有条件(使用'?.')或向目标添加空检查('!')。错误。
无法无条件访问属性'longitude',因为接收者可以是'null'。
尝试使访问有条件(使用'?.')或向目标添加空检查('!')。错误
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeApp(),
);
}
}
class HomeApp extends StatefulWidget {
HomeApp({Key? key}) : super(key: key);
@override
State<HomeApp> createState() => _HomeAppState();
}
class _HomeAppState extends State<HomeApp> {
String currentAddress = 'my address';
Position? currentPosition;
Future<Position?> _determinePosition() async {
bool serviceEnabled;
LocationPermission permission;
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
Fluttertoast.showToast(msg: 'Please keep your location on.');
}
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
Fluttertoast.showToast(msg: 'Location permission is denied.');
}
}
if (permission == LocationPermission.deniedForever) {
Fluttertoast.showToast(msg: 'Location permission is denies forever.');
}
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
try {
List<Placemark> placemark =
await placemarkFromCoordinates(position.latitude, position.longitude);
Placemark place = placemark[0];
setState(() {
currentPosition = position;
currentAddress =
"${place.locality},${place.postalCode},${place.country}";
});
} catch (e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Location App'),
),
body: Center(
child: Column(
children: [
Text(
currentAddress,
style: TextStyle(
fontSize: 20,
),
),
(currentPosition != null)
? Text(
"Latitude: " + currentPosition.latitude.toString(),
)
: Container(),
(currentPosition != null)
? Text(
"Longitude: " + currentPosition.longitude.toString(),
)
: Container(),
TextButton(
onPressed: () {
_determinePosition();
},
child: Text('Locate Me'),
),
],
),
),
);
}
}
改为:
currentPosition!.latitude.toString()
由于您检查 currentPosition 不为空,您可以使用 !
来表示“我知道它可以为空,但我也知道此时它肯定不是” .
我在 Text( "纬度:" + currentPosition.latitude.toString(), ) 这一行。我不知道如何让 currentPosition.latitude not null 帮助我解决这些问题。
它给我这个 属性 'latitude' 不能无条件访问因为接收者可以是 'null'。 尝试使访问有条件(使用'?.')或向目标添加空检查('!')。错误。
无法无条件访问属性'longitude',因为接收者可以是'null'。 尝试使访问有条件(使用'?.')或向目标添加空检查('!')。错误
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeApp(),
);
}
}
class HomeApp extends StatefulWidget {
HomeApp({Key? key}) : super(key: key);
@override
State<HomeApp> createState() => _HomeAppState();
}
class _HomeAppState extends State<HomeApp> {
String currentAddress = 'my address';
Position? currentPosition;
Future<Position?> _determinePosition() async {
bool serviceEnabled;
LocationPermission permission;
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
Fluttertoast.showToast(msg: 'Please keep your location on.');
}
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
Fluttertoast.showToast(msg: 'Location permission is denied.');
}
}
if (permission == LocationPermission.deniedForever) {
Fluttertoast.showToast(msg: 'Location permission is denies forever.');
}
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
try {
List<Placemark> placemark =
await placemarkFromCoordinates(position.latitude, position.longitude);
Placemark place = placemark[0];
setState(() {
currentPosition = position;
currentAddress =
"${place.locality},${place.postalCode},${place.country}";
});
} catch (e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Location App'),
),
body: Center(
child: Column(
children: [
Text(
currentAddress,
style: TextStyle(
fontSize: 20,
),
),
(currentPosition != null)
? Text(
"Latitude: " + currentPosition.latitude.toString(),
)
: Container(),
(currentPosition != null)
? Text(
"Longitude: " + currentPosition.longitude.toString(),
)
: Container(),
TextButton(
onPressed: () {
_determinePosition();
},
child: Text('Locate Me'),
),
],
),
),
);
}
}
改为:
currentPosition!.latitude.toString()
由于您检查 currentPosition 不为空,您可以使用 !
来表示“我知道它可以为空,但我也知道此时它肯定不是” .