【问题标题】:Error while getting current location in flutter在颤动中获取当前位置时出错
【发布时间】:2020-04-23 20:25:26
【问题描述】:

我试图在我的颤振应用程序中获取当前位置,该应用程序正在执行,但稍后在移动屏幕上它向我抛出一个错误,如 nosuchmethoderror 并且在我的终端上它向我抛出一个错误。有人可以帮我解决这个问题.

这是我的代码:

class LocationService {
  // Keep track of current Location
  UserLocation _currentLocation;
  Location location = Location();
  // Continuously emit location updates
  StreamController<UserLocation> _locationController =
      StreamController<UserLocation>.broadcast();

  LocationService() {
    location.requestPermission().then((granted) {
      if (granted) {
        location.onLocationChanged().listen((locationData) {
          if (locationData != null) {
            _locationController.add(UserLocation(
              latitude: locationData.latitude,
              longitude: locationData.longitude,
            ));
          }
        });
      }
    });
  }

  Stream<UserLocation> get locationStream => _locationController.stream;

  Future<UserLocation> getLocation() async {
    try {
      var userLocation = await location.getLocation();
      _currentLocation = UserLocation(
        latitude: userLocation.latitude,
        longitude: userLocation.longitude,
      );
    } catch (e) {
      print('Could not get the location: $e');
    }

    return _currentLocation;
  }
}

这是我的错误:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (27476): The following NoSuchMethodError was thrown building HomeView(dirty, dependencies:
I/flutter (27476): [InheritedProvider<UserLocation>]):
I/flutter (27476): The getter 'latitude' was called on null.
I/flutter (27476): Receiver: null
I/flutter (27476): Tried calling: latitude
I/flutter (27476): 
I/flutter (27476): The relevant error-causing widget was:
I/flutter (27476):   HomeView
I/flutter (27476):   file:///C:/Users/GANGADHAR%20YADAV/AndroidStudioProjects/027-location-service/lib/main.dart:16:57
I/flutter (27476): 
I/flutter (27476): When the exception was thrown, this was the stack:
I/flutter (27476): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
I/flutter (27476): #1      HomeView.build (package:location_service/views/home_view.dart:14:43)
I/flutter (27476): #2      StatelessElement.build (package:flutter/src/widgets/framework.dart:4291:28)
I/flutter (27476): #3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)
I/flutter (27476): #4      Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter (27476): #5      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
I/flutter (27476): #6      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
I/flutter (27476): #7      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
I/flutter (27476): #8      Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
I/flutter (27476): #9      SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
I/flutter (27476): #10     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
I/flutter (27476): #11     Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)

Main.dart

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return StreamProvider<UserLocation>(
        builder: (context) => LocationService().locationStream,
        child: MaterialApp(title: 'Flutter Demo', home: HomeView()));
  }
}

用户位置.dart

class UserLocation {
  final double latitude;
  final double longitude;

  UserLocation({this.latitude, this.longitude});
}

【问题讨论】:

  • 你能告诉我我该怎么做吗
  • 您能出示您的HomeView 代码吗?
  • 我已经更新了我的代码,请检查
  • 这是因为您的纬度正在获取空值以尝试放置断点或处于调试模式,并让我知道您在哪里得到空纬度
  • 你能告诉我在哪里可以添加断点

标签: flutter dart


【解决方案1】:

从 onLocationChanged 中删除“()”。我希望它能消除错误。请参阅位置包文档。

【讨论】:

    猜你喜欢
    • 2019-06-08
    • 2023-04-01
    • 2016-08-06
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    相关资源
    最近更新 更多