引发了另一个异常:NoSuchMethodError:在null上调用了getter'latitude'

编程入门 行业动态 更新时间:2024-10-22 15:26:01
本文介绍了引发了另一个异常:NoSuchMethodError:在null上调用了getter'latitude'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用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> [ Imagework(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> [ Imagework(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'

本文发布于:2023-11-11 23:31:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579842.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用了   引发了   异常   NoSuchMethodError   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!