我使用数组存储“市场位置数据”,并将数据保存在Data.dart文件中
Data.dart
class Market{
static List dataMarket = [
Data(
nameDest = 'Alfamart',
latitudeDest: -6.163891,
longitudeDest: 106.628915),
Data(
nameDest = 'Minis'
latitudeDest: -6.211269,
longitudeDest: 106.615574),
Data(
nameDest = 'Star Market'
latitudeDest: -6.223254,
longitudeDest: 106.703905),
Data(
nameDest = 'Allmark'
latitudeDest: -6.163891,
longitudeDest: 106.628915),
Data(
nameDest = 'Eig Store'
latitudeDest: -6.178293,
longitudeDest: 106.631862),
];
}
class Data{
String nameDest;
var latitudeDest;
var longitudeDest;
Data({
this.nameDest
this.latitudeDest,
this.longitudeDest});
}
我使用“市场位置数据”来计算用户和市场之间的距离。而且我还想显示最近的市场订单
MyListMarket.dart
import 'package:example/Data.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:math' show cos, sqrt, asin;
import 'package:geolocator/geolocator.dart';
class TabMarket extends StatefulWidget{
@override
_TabMarketState createState() => _TabMarketState();
}
class _TabMarketState extends State<TabMarket> {
var lastLatitude;
var lastLongitude;
@override
void initState() {
super.initState();
_initUserLocation();
}
Future<void> _initUserLocation() async {
try {
Position position = await Geolocator()
.getLastKnownPosition(desiredAccuracy: LocationAccuracy.best);
lastLatitude = position.latitude;
lastLongitude = position.longitude;
} catch (e) {
print("Permission Denied");
}
}
@override
Widget build(BuildContext context) {
final dataInstance = Market.dataMarket;
var newSortRange = [];
if (lastLatitude == null || lastLongitude == null){
for(int i = 0; i >= dataInstance.length; i++) {
newSortRange[i] = 0;
}
}else{for (int i = 0; i >= dataInstance.length; i++) {
//calculating distance with lat1, lon1, lat2, lon2
var p = 0.017453292519943295;
var c = cos;
var a = 0.5 - c((dataInstance[i].latitudeDest - lastLatitude) * p) / 2 +
c(lastLatitude * p) * c(dataInstance[i].latitudeDest * p) *
(1 - c((dataInstance[i].longitudeDest - lastLongitude) * p)) / 2;
newSortRange[i] = 12742 * asin(sqrt(a));
}}
newSortRange.sort();
//i wanna sort this data from the smallest number
//already tried with newSortRange.sort((a,b) => a.compareTo(b));
return Scaffold(
backgroundColor: Color.fromRGBO(246, 246, 246, 1),
body: FutureBuilder(
future: _initUserLocation(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
return ListView.separated(
key: PageStorageKey<String>('aKey'),
physics: BouncingScrollPhysics(),
itemCount: newSortRange.length,
itemBuilder: (context, index) {
return Material(
color: Color.fromRGBO(251, 251, 251, 1),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ListTile(
leading: Text(dataInstance[index].nameDest)
trailing: Text(newSortRange[index].toStringAsFixed(2) +
"\nKM",
style: new TextStyle(fontSize: 13,),
textAlign: TextAlign.right,),
),
],),);
},
separatorBuilder: (context, index) {
return Divider(
height: 0.0,
thickness: 0.7,
);
}
);
},
),
);
}
}
所以我尝试用for循环计算距离并将其保存到新数组中
我的代码没有错误,但没有列表视图就什么也没有出现。 也许将数据存储到数组中的方法是错误的? 我该如何解决?
答案 0 :(得分:0)
由于_initUserLocation方法是异步的,因此即使您以initstate进行调用,也无法基于build方法中的值来计算其他值,因为_initUserLocation方法无法完成其任务。
在将来的构建器中,您必须计算可以看到实际数据的所有内容,因为将来的构建器会在过程完成时提供数据。
您必须从future方法返回任何数据,以便futurebuilder现在可以了解方法的完整计算。
我对您的代码进行了许多更改,效果很好。
让我知道它是否对您有用,如果您不了解任何代码部分,也可以随时提出任何问题。
完整演示:
class _DeleteWidgetState extends State<DeleteWidget> {
var lastLatitude;
var lastLongitude;
final dataInstance = Market.dataMarket;
var newSortRange = [];
@override
void initState() {
super.initState();
//_initUserLocation();
}
Future<int> _initUserLocation() async {
try {
await Future.delayed(Duration(seconds: 1));
Position position = await Geolocator()
.getLastKnownPosition(desiredAccuracy: LocationAccuracy.best);
lastLatitude = position.latitude;
lastLongitude = position.longitude;
} catch (e) {
print("Permission Denied");
}
return 1;
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color.fromRGBO(246, 246, 246, 1),
body: FutureBuilder(
future: _initUserLocation(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
if (lastLatitude == null || lastLongitude == null) {
for (int i = 0; i < dataInstance.length; i++) {
newSortRange[i] = 0;
}
} else {
for (int i = 0; i < dataInstance.length; i++) {
//calculating distance with lat1, lon1, lat2, lon2
var p = 0.017453292519943295;
var c = cos;
var a = 0.5 -
c((dataInstance[i].latitudeDest - lastLatitude) * p) / 2 +
c(lastLatitude * p) *
c(dataInstance[i].latitudeDest * p) *
(1 -
c((dataInstance[i].longitudeDest - lastLongitude) *
p)) /
2;
newSortRange.add(12742 * asin(sqrt(a)));
}
}
newSortRange.sort();
print(newSortRange);
return ListView.separated(
key: PageStorageKey<String>('aKey'),
physics: BouncingScrollPhysics(),
itemCount: newSortRange.length,
itemBuilder: (context, index) {
return Material(
color: Color.fromRGBO(251, 251, 251, 1),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ListTile(
trailing: Text(
newSortRange[index].toStringAsFixed(2) + "\nKM",
style: new TextStyle(
fontSize: 13,
),
textAlign: TextAlign.right,
),
),
],
),
);
},
separatorBuilder: (context, index) {
return Divider(
height: 0.0,
thickness: 0.7,
);
});
},
),
);
}
}
class Market {
static List dataMarket = [
Data(latitudeDest: -6.163891, longitudeDest: 106.628915),
Data(latitudeDest: -6.211269, longitudeDest: 106.615574),
Data(latitudeDest: -6.223254, longitudeDest: 106.703905),
Data(latitudeDest: -6.163891, longitudeDest: 106.628915),
Data(latitudeDest: -6.178293, longitudeDest: 106.631862),
];
}
class Data {
var latitudeDest;
var longitudeDest;
Data({this.latitudeDest, this.longitudeDest});
}