目前正在学习扑动和得到这个错误,而试图检测到设备的位置:
类型“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
貌似教程使用location
插件的旧版本完成,因为V2.0.0他们改变了API返回结构化数据,而不是地图。
https://github.com/Lyokone/flutterlocation/blob/master/CHANGELOG.md
所以,你需要更改你的所有Map<String, double>
类型LocationData
或设置你的插件版本^1.4.0
。
我尝试过很多办法,直到我发现多亏了谁帮助另一个扑Facebook的group.Make确保您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-更多的选择