首页 > 解决方案 > 如何在 Flutter 上的 StreamBuilder 之前调用 Future 方法?

问题描述

我试图在使用该信息获取流数据之前获取当前用户位置。所以使用 Flutter 1.17.5geolocator: ^5.3.2+2我正在Position使用 follow util 类

class LocationProvider {

Future<Position> getPosition() async {
    Position position = await Geolocator()
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    return position;
  }
}

然后我必须将信息传递给bloc才能使用标记检索流数据

Widget _getAllProducts(ProductBloc productBloc, LocationProvider locationProvider) async{
Position position = locationProvider.getPosition();
var longitude = position.longitude;
var latitude = position.latitude;
int meter = 50;
productBloc.allByRange(meter, latitude, longitude);

return StreamBuilder(
  stream: productBloc.productsByRangeStream,

我是怎么收到这个消息的

A value of type 'StreamBuilder<List<ProductModel>>' can't be returned from method '_getAllProducts' because it has a return type of 'Widget'

编辑 1

class ProductBloc {

Stream<List<ProductModel>> get productsByRangeStream =>
  _allByRangeProductsController.stream;



void allByRange(double range, double lat, double lon) async {
    final products = await _productFirebaseService.allByRange(range, lat, lon);
    _allByRangeProductsController.sink.add(products);
  }

}

我该如何解决?

标签: flutterdartgeolocationfuturestream-builder

解决方案


推荐阅读