【问题标题】:'StreamSubscription<LocationData>' cannot be assigned to 'StreamSubscription<Map<String, double>>''StreamSubscription<LocationData>' 不能分配给 'StreamSubscription<Map<String, double>>'
【发布时间】:2019-02-11 11:52:58
【问题描述】:

目前正在学习 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

【问题讨论】:

    标签: dart flutter location


    【解决方案1】:

    看起来本教程是使用旧版本的 location 插件完成的,从 v2.0.0 开始,他们更改了 api 以返回结构化数据而不是地图。

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

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

    【讨论】:

      【解决方案2】:

      我尝试了很多方法,直到我找到了这种方法,这要感谢一个帮助另一个颤振 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-

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-01
        • 2019-10-07
        • 1970-01-01
        • 2014-08-15
        • 2020-02-24
        • 2021-05-26
        相关资源
        最近更新 更多