无法将'StreamSubscription <locationdata>'分配给'StreamSubscription <map <string,double =“” >>'

时间:2019-02-11 11:52:59

标签: dart flutter location

当前正在学习Flutter,并在尝试检测设备位置时遇到此错误:

  

无法分配“ StreamSubscription”类型的值   到类型为'StreamSubscription>'的变量

我正在关注在线教程,但不知何故出现了此错误。

import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:location/location.dart';
import 'dart:async';
import 'package:flutter/services.dart';

class MainPage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() => AppState();
} 

class AppState extends State<MainPage> {

  Map<String,double> currentLocation = new Map();
  StreamSubscription<Map<String,double>> locationSubscription;

  var location = new Location();
  String error;

  void initState() {
    super.initState();

    currentLocation['latitude'] = 0.0;
    currentLocation['longitude'] = 0.0;

    initPlatformState();

    locationSubscription = 
    location.onLocationChanged().listen((Map<String,double> result) {
      setState(() {
        currentLocation = result; 
      });
    });
  }

  void initPlatformState() async{
    Map<String,double> myLocation;
    try {
      myLocation = await location.getLocation();
      error="";
    } on PlatformException catch(e) {
      if(e.code == 'PERMISSION_DENIED')
        error = "permission denied";
      else if(e.code == "PERMISSION_DENIED_NEVER_ASK")
        error = "permission denied";
      myLocation = null;
    }

    setState(() {
      currentLocation = myLocation; 
    });
  }

  @override
  Widget build (BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(""),
        automaticallyImplyLeading: false,
      ),
      body: Container(
        child: FlutterMap(
          options: MapOptions(
          ),
          layers: [
            TileLayerOptions(

            ),
          ]
        ),
      )
    ); 
  } 
}

我将非常感谢您的任何建议。这是我关注的视频:https://www.youtube.com/watch?v=K4nYTayjofY&t=321s

2 个答案:

答案 0 :(得分:3)

该教程似乎是使用location插件的较旧版本完成的,因为v2.0.0以来,他们将api更改为返回结构化数据而非地图。

https://github.com/Lyokone/flutterlocation/blob/master/CHANGELOG.md

因此,您需要将所有Map<String, double>类型更改为LocationData或将插件版本设置为^1.4.0

答案 1 :(得分:0)

我一直尝试了很多方法,直到我找到了这种方法,这要归功于一个善良的人帮助了另一个扑通的Facebook组。确保在pubspec.yaml中将位置更新为最新版本

dependencies:
  location: ^2.3.5

然后将其更改为以下代码:

  LocationData _currentLocation;
  StreamSubscription<LocationData> _locationSubscription;

  var _locationService = new Location();
  String error;

  void initState() {
    super.initState();

    initPlatformState();

    _locationSubscription = _locationService
        .onLocationChanged()
        .listen((LocationData currentLocation) async {
      setState(() {
        _currentLocation = currentLocation;
      });
    });
  }

  void initPlatformState() async {
    try {
      _currentLocation = await _locationService.getLocation();


    } on PlatformException catch (e) {
      if (e.code == 'PERMISSION_DENIED') {
        error = 'Permission denied';
      }else if(e.code == "PERMISSION_DENIED_NEVER_ASK"){
        error = 'Permission denied';
      }
      _currentLocation = null;
    }

您可以将经度和纬度输入为

_currentLocation.longitude和_currentLocation.latitude

这些将返回双精度值。此外,还有更多可用选项 https://pub.dev/packages/location#-readme-tab-