Flutter 每次接收到新位置时,以集团模式重新创建地图
我正在尝试在地图应用程序中使用。应用程序启动时,首先找到用户位置,然后将地图中心更改为用户位置。 当用户移动时,我想更改地图上的标记。这是我的代码:Flutter 每次接收到新位置时,以集团模式重新创建地图,flutter,bloc,Flutter,Bloc,我正在尝试在地图应用程序中使用。应用程序启动时,首先找到用户位置,然后将地图中心更改为用户位置。 当用户移动时,我想更改地图上的标记。这是我的代码: class HomeFoursquareScreen extends StatelessWidget { @override Widget build(BuildContext context) { return BlocBuilder<LocationBloc, LocationState>( builde
class HomeFoursquareScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<LocationBloc, LocationState>(
builder: (context, locationState) {
if (locationState is LocationLoadingState) {
return const Center(child: CircularProgressIndicator());
} else if (locationState is LocationLoadedState) {
return _MyMap(locationState.location);
} else {
return const Text('oops...something went wrong');
}
},
);
}
}
class _MyMap extends StatelessWidget {
const _MyMap({Key key, @required this.location}) : super(key: key);
final Location location;
@override
Widget build(BuildContext context) {
return BlocBuilder<FourSquareBloc, FourSquareState>(
builder: (context, foursquareState) {
return FlutterMap(...);
}
);
},
)
class HomeFoursquareScreen扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回BlocBuilder(
生成器:(上下文,位置状态){
if(locationState为LocationLoadingState){
返回常量中心(子项:CircularProgressIndicator());
}else if(locationState为LocationLoadedState){
返回MyMap(locationState.location);
}否则{
返回常量文本('oops…出错了');
}
},
);
}
}
类MyMap扩展了无状态小部件{
const_MyMap({Key-Key,@required this.location}):super(Key:Key);
最终位置;
@凌驾
小部件构建(构建上下文){
返回BlocBuilder(
生成器:(上下文,foursquareState){
返回图(…);
}
);
},
)
但正如您在我的代码中看到的,每次更改我的位置时,这意味着当用户移动时,LocationLoadedState
会被触发,\u MyMap
小部件会被调用,并且flatterMap
会被重新创建!!我认为持续创建map的新实例对性能不好,也不符合逻辑!对吗什么是正确的方法
我想成为固定地图,但其他东西,如MapOptions或Marker,在新数据到达时获取数据。继承的Widget/Provider.of方法如何?与bloc或我的问题有什么关系?@GeorgeLee继承的Widget/Provider.of方法如何?与bloc或我的问题有什么关系?@GeorgeLee