gpt4 book ai didi

dart - 'StreamSubscription' cannot be assigned to ' StreamSubscription>'

转载 作者:IT王子 更新时间:2023-10-29 06:38:09 27 4
gpt4 key购买 nike

目前正在学习 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 以返回结构化数据而不是 map 。

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

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

关于dart - 'StreamSubscription<LocationData >' cannot be assigned to ' StreamSubscription<Map<String, double>>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54630038/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com