【问题标题】:Another exception was thrown: NoSuchMethodError: The getter 'latitude' was called on null引发了另一个异常:NoSuchMethodError: The getter 'latitude' was called on null
【发布时间】:2020-06-26 14:17:11
【问题描述】:

我正在尝试在 Flutter 中使用 Location 包,但是当我尝试在我的应用程序中显示纬度和经度时,我收到一条错误消息,指出“在 null 上调用了 getter 'latitude'。”我的设备上的定位服务可能有问题吗?当我点击屏幕“为了获得更好的体验,打开设备位置,它使用谷歌的定位服务”时,我也会弹出一个提示,但我已经在应用程序中启用了定位服务的权限。下面是我的代码

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}')

            ]
          )
        )
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart dart-pub


    【解决方案1】:

    尝试使用 future builder 进行异步方法回调,例如

    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()完全执行之前被调用

    【讨论】:

      【解决方案2】:

      您正在尝试在异步调用结束之前访问纬度。您在initState() 上调用它,但该方法可能在build() 函数执行后结束,因此在尝试构建Text('Latitude: ${locationData.latitude}') 小部件时,locationData 变量为null。一个快速的解决方法是在locationData 上设置一个默认值,但如果你想“等待”直到retrieveLocation() 方法结束,你可以:

        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"]), 
                    locationData!=null? Text('Latitude: ${locationData.latitude}') : Text('Waiting Latitude'),
                    locationData!=null? Text('Longitude: ${locationData.longitude}') : Text('Waiting Longitude'),
                  ]
                )
              )
            ),
          );
        }
      

      【讨论】:

        猜你喜欢
        • 2019-12-05
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 2021-12-31
        • 2019-12-06
        • 2019-12-10
        • 2021-08-03
        • 2022-01-16
        相关资源
        最近更新 更多