【发布时间】:2021-02-14 05:43:21
【问题描述】:
自从我开始为这个问题苦苦挣扎已经一周了,所以我完全不知所措。 在我的代码中 - 请参见下文 - 我需要解析两个期货来构建页面。一个 - 从 Geolocator 包获取地图的用户位置,另一个是该位置用户可用的工作/活动列表。活动/工作未来也依赖于获取用户的位置,然后获取数据表单 API。
我面临的问题并尝试了几种方法(Location 和 Geolocator 包),但我得到的问题是 futureSnapshot 连接完成(转到“完成”)但未来的位置部分为空。 即使我根据 Pieter 的建议创建了一个 Future 方法,我仍然得到
“W/Geolocator(12158):位置权限不是权限的一部分 发送到 onRequestPermissionsResult 方法。 W/IInputConnectionWrapper(12158):reportFullscreenMode 不存在 输入连接"
创建的 Future 现在被 fetchJobsFuture 使用并提供给 MapCard 小部件
import 'package:geolocator/geolocator.dart';
Future<Position> getUserPosition() async {
var _permissionGranted = await Geolocator.checkPermission();
if (_permissionGranted != LocationPermission.always ||
_permissionGranted != LocationPermission.whileInUse) {
await Geolocator.requestPermission();
}
final position = await Geolocator.getCurrentPosition();
return position;
}
这是页面的全部代码:
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
static const double _desiredSplit =
0.6; // ONLY one after the decimal point!!!
double currentSplit = _desiredSplit;
static const double _minMapFragment = 0.04;
double listTopBarHeight = 42.0;
Future<JobList> fetchJobsFuture;
JobList currentListOfJobs;
Future<Position> userPositionFuture;
Position userPosition;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
currentListOfJobs = Provider.of<JobList>(context, listen: false);
getUserPosition().then((value) => userPosition = value).then((_) {
fetchJobsFuture = currentListOfJobs.getCurrentJobList();
});
return Scaffold(
backgroundColor: Colors.white,
body: FutureBuilder(
future: fetchJobsFuture,
builder: (context, AsyncSnapshot<JobList> futureSnaphot) {
if (!futureSnaphot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
} else {
return Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(
height: 200,
child: Container(
child: MapCard(
latitude: userPosition.latitude,
longitude: userPosition.longitude,
),
),
),
SizedBox(
height: 300,
child: ListCard(
jobList: futureSnaphot.data,
),
),
],
);
}
}),
);
}
}
任何想法或建议将不胜感激 - 谢谢!干杯!
【问题讨论】:
标签: flutter geolocation