'StreamSubscription<LocationData>' 无法分配给 'StreamSubscription<Map<String, double>>'
'StreamSubscription<LocationData>' cannot be assigned to 'StreamSubscription<Map<String, double>>'
目前正在学习 Flutter 并在尝试检测我的设备位置时遇到此错误:
A value of type 'StreamSubscription' can't be assigned
to a variable of type '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 更改为 return 结构化数据而不是地图。
https://github.com/Lyokone/flutterlocation/blob/master/CHANGELOG.md
因此您需要将所有 Map<String, double>
类型更改为 LocationData
或将插件版本设置为 ^1.4.0
.
我尝试了很多方法,直到我找到了这个方法,这要感谢一位在另一个 flutter 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
这些将 return 加倍值。此外,还有更多选项可用
https://pub.dev/packages/location#-readme-tab-
目前正在学习 Flutter 并在尝试检测我的设备位置时遇到此错误:
A value of type 'StreamSubscription' can't be assigned to a variable of type '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 更改为 return 结构化数据而不是地图。
https://github.com/Lyokone/flutterlocation/blob/master/CHANGELOG.md
因此您需要将所有 Map<String, double>
类型更改为 LocationData
或将插件版本设置为 ^1.4.0
.
我尝试了很多方法,直到我找到了这个方法,这要感谢一位在另一个 flutter 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
这些将 return 加倍值。此外,还有更多选项可用 https://pub.dev/packages/location#-readme-tab-