无法将'StreamSubscription <LocationData>'分配给'StreamSubscription <Map <String,double >>'

Jus*_*Ren 2 location dart flutter

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

            ),
          ]
        ),
      )
    ); 
  } 
}
Run Code Online (Sandbox Code Playgroud)

如有任何建议,我将不胜感激。这是我关注的视频:https : //www.youtube.com/watch?v=K4nYTayjofY&t=321s

Jor*_*ies 5

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

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

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