引发了另一个异常:NoSuchMethodError:在null上调用了getter'latitude' [英] Another exception was thrown: NoSuchMethodError: The getter 'latitude' was called on null
问题描述
我正在尝试使用Flutter中的Location软件包,但是当我尝试在我的应用中显示纬度和经度时,我收到一条错误消息,指出"getter'纬度'被调用为null."我的设备上的位置服务是否可能有问题?当我单击屏幕上的消息时,还会提示我一个弹出窗口,提示为了获得更好的体验,请打开使用Google位置服务的设备位置",但是我已经在应用程序中启用了位置服务的权限.以下是我的代码
I am trying to utilize the Location package in Flutter, but when I try to display the Latitude and Longitude in my app I am getting an error stating "The getter 'latitude' was called on null." Is it possible that there's something wrong with the location services on my device? I am also prompted with a popup when I click on the screen stating "For a better experience, turn on device location, which uses Google's location service" but I already enabled permissions for location services in the app. Below is my code
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:provider/provider.dart';
import 'package:wasteagram/data/firestore_service.dart';
import 'package:location/location.dart';
import 'dart:async';
import 'package:wasteagram/model/user_location.dart';
class DetailPage extends StatefulWidget {
final DocumentSnapshot post;
DetailPage({this.post});
@override
_DetailPageState createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
LocationData locationData;
@override
void initState() {
super.initState();
retrieveLocation();
}
void retrieveLocation() async {
var locationService = Location();
locationData = await locationService.getLocation();
setState(() {
});
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.post.data["wastedate"])
),
body: Center(
child: Container(
child: Column(
children: <Widget> [
Image.network(widget.post.data["image"]),
Text(widget.post.data["wastedate"]),
Text(widget.post.data["wastenumber"]),
Text('Latitude: ${locationData.latitude}'),
Text('Longitude: ${locationData.longitude}')
]
)
)
),
);
}
}
推荐答案
尝试将将来的构建器用于异步方法回调,例如
try to use future builder for async method callback e.g.
FutureBuilder(
future: retrieveLocation(),
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return Center(child: CircularProgressIndicator());
}
return Center(
child: Container(
child: Column(
children: <Widget> [
Image.network(widget.post.data["image"]),
Text(widget.post.data["wastedate"]),
Text(widget.post.data["wastenumber"]),
Text('Latitude: ${locationData.latitude}'),
Text('Longitude: ${locationData.longitude}')
]
)
)
},
);
问题是在完全执行 retrieveLocation()
这篇关于引发了另一个异常:NoSuchMethodError:在null上调用了getter'latitude'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!