Flutter 颤振FutureProvider值未在生成器方法中更新 问题
我正在Flitter中构建一个基本应用程序,它可以获取用户的位置,并以类似Tinder的刷卡格式显示附近的位置。我成功地实现了地理定位,但是当使用FutureProvider/Consumer时,我遇到了一个奇怪的错误,用户到位置的相对距离被卡组中的第一个距离值覆盖。虽然我对flatter和Provider包还不熟悉,但我相信有一个简单的解决方法 旁注:在谷歌上搜索之后,我试图使用FutureProvider.value()阻止旧值更新,但没有成功 提前感谢您提供的任何帮助或指导 快速演示 使用的软件包 刷卡省道Flutter 颤振FutureProvider值未在生成器方法中更新 问题,flutter,dart,flutter-provider,Flutter,Dart,Flutter Provider,我正在Flitter中构建一个基本应用程序,它可以获取用户的位置,并以类似Tinder的刷卡格式显示附近的位置。我成功地实现了地理定位,但是当使用FutureProvider/Consumer时,我遇到了一个奇怪的错误,用户到位置的相对距离被卡组中的第一个距离值覆盖。虽然我对flatter和Provider包还不熟悉,但我相信有一个简单的解决方法 旁注:在谷歌上搜索之后,我试图使用FutureProvider.value()阻止旧值更新,但没有成功 提前感谢您提供的任何帮助或指导 快速演示 使
导入“包装:颤振/材料.省道”;
导入“包:地理定位器/地理定位器.dart”;
导入“包:provider/provider.dart”;
导入“package:swipe_stack/swipe_stack.dart”;
导入“../services/geologitor_service.dart”;
导入“../models/place.dart”;
类CardSwipe扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
final _currentPosition=Provider.of(上下文);
final _placesProvider=提供者(上下文);
最终_geoService=geologitorService();
返回未来提供者(
创建:(上下文)=>\u placesProvider,
孩子:脚手架(
背景颜色:颜色。灰色[300],
正文:(_currentPosition!=null)
?消费者(
建筑商:(uu,地点,uuu){
返回(位置!=null)
?立柱(
儿童:[
尺寸箱(高度:10.0),
容器(
边距:仅限边缘集(顶部:120.0),
身高:600,
孩子:SwipeStack(
子对象:地点。地图((地点){
返回SwiperItem(生成器:
(开关位置,
(双倍进度){
返回未来提供者(
创建:(上下文)=>
_geoService.getDistance(
_当前位置。纬度,
_当前位置。经度,
place.geometry.location.lat,
位置、几何结构、位置、液化天然气),
儿童:消费者(
建筑商:(u,距离,uu){
返回(距离!=null)
?中心(
孩子:卡片(
子:容器(
身高:200,
宽度:200,
儿童:中心(
子:列(
主轴对准:
主轴对准
居中
儿童:[
文本(地点、名称),
正文(
“${(距离/1609).toStringAsFixed(3)}mi'),//将米转换为米
],
),
),
),
),
)
:容器();
}),
);
});
}).toList(),
访问次数:3,
stackFrom:stackFrom.Top,
翻译时间:6,
比例区间:0.03,
onEnd:()=>debugPrint(“onEnd”),
onSwipe:(内部索引,开关位置)=>
debugPrint(“onswip$index$position”),
正在倒带:
(内部索引,开关位置)=>
debugPrint(“倒带$index$位置”),
),
),
],
)
:中心(
子对象:CircularProgressIndicator(),
);
},
)
:中心(
子对象:CircularProgressIndicator(),
),
),
);
}
}
地理定位服务
import'包:geologitor/geologitor.dart';
类地理定位服务{
最终地理定位器=地理定位器();
Future getLocation()异步{
返回等待geolocator.getCurrentPosition(
所需精度:定位精度高,
locationPermissionLevel:GeolocationPermission.location,
);
}
未来getDistance(
双跳、双跳、双端LAT、双端LNG)异步{
返回等待地理定位器之间的距离(startLat、STARTING、endLat、endLng);
}
}
飞镖
快速提示:<
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:provider/provider.dart';
import 'package:swipe_stack/swipe_stack.dart';
import '../services/geolocator_service.dart';
import '../models/place.dart';
class CardSwipe extends StatelessWidget {
@override
Widget build(BuildContext context) {
final _currentPosition = Provider.of<Position>(context);
final _placesProvider = Provider.of<Future<List<Place>>>(context);
final _geoService = GeoLocatorService();
return FutureProvider(
create: (context) => _placesProvider,
child: Scaffold(
backgroundColor: Colors.grey[300],
body: (_currentPosition != null)
? Consumer<List<Place>>(
builder: (_, places, __) {
return (places != null)
? Column(
children: [
SizedBox(height: 10.0),
Container(
margin: EdgeInsets.only(top: 120.0),
height: 600,
child: SwipeStack(
children: places.map((place) {
return SwiperItem(builder:
(SwiperPosition position,
double progress) {
return FutureProvider(
create: (context) =>
_geoService.getDistance(
_currentPosition.latitude,
_currentPosition.longitude,
place.geometry.location.lat,
place.geometry.location.lng),
child: Consumer<double>(
builder: (_, distance, __) {
return (distance != null)
? Center(
child: Card(
child: Container(
height: 200,
width: 200,
child: Center(
child: Column(
mainAxisAlignment:
MainAxisAlignment
.center,
children: [
Text(place.name),
Text(
'${(distance / 1609).toStringAsFixed(3)} mi'), // convert meter to mi
],
),
),
),
),
)
: Container();
}),
);
});
}).toList(),
visibleCount: 3,
stackFrom: StackFrom.Top,
translationInterval: 6,
scaleInterval: 0.03,
onEnd: () => debugPrint("onEnd"),
onSwipe: (int index, SwiperPosition position) =>
debugPrint("onSwipe $index $position"),
onRewind:
(int index, SwiperPosition position) =>
debugPrint("onRewind $index $position"),
),
),
],
)
: Center(
child: CircularProgressIndicator(),
);
},
)
: Center(
child: CircularProgressIndicator(),
),
),
);
}
}
import 'package:geolocator/geolocator.dart';
class GeoLocatorService {
final geolocator = Geolocator();
Future<Position> getLocation() async {
return await geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high,
locationPermissionLevel: GeolocationPermission.location,
);
}
Future<double> getDistance(
double startLat, double startLng, double endLat, double endLng) async {
return await geolocator.distanceBetween(startLat, startLng, endLat, endLng);
}
}
import './geometry.dart';
class Place {
final String name;
final Geometry geometry;
Place(this.name, this.geometry);
Place.fromJson(Map<dynamic, dynamic> parsedJson)
: name = parsedJson['name'],
geometry = Geometry.fromJson(
parsedJson['geometry'],
);
}