函数处理后如何获取某些数据
How can i fetch Certain data after being processed by a function
飞镖
我将纬度和经度作为 `currentUser 。我在 firestore 中有文档每个用户文档都有自己的经度和纬度,
我还有一个方法可以得到两点之间的距离
getDistance(){
double distanceInMeters = Geolocator.distanceBetween(MyLongitude , myLatitude ,otherUserlatitude , otherUserLongiTude );
}
我也有这个简单的 Stream.BuilderWidget 和 listview.builder
StreamBuilder<QuerySnapshot>(stream: FirebaseFirestore.instance.collection(currentUser.uid).orderBy("timestamp",descending: true).snapshots(),
builder: (context , snapshot) {
if (!snapshot.hasData) {
return Center(
child: circulearProgress(),
);
}
return ListView.builder(
itemCount: snapshot.data.docs.length ,
itemBuilder: (context , int index){
DocumentSnapshot documentSnapshot = snapshot.data.docs [index] ;
return
// well i need to put the result of distance here after processing in the last method i mentioned (into Text.ToString)
);
}
)
首先处理成特定方法后,如何将数据提取到 ListView.Bulder
换句话说,我需要先在方法中处理数据,然后将结果提取到 listview.bulder
我不能处理这个因为我是初学者
或者一般来说,数据在listview.bulider或者Stream内部启动之前,如何处理成method。事不限远只
先谢谢各位朋友了
将 getDistance
方法更新为:
double getDistance({@required double myLatitude, @required double myLongitude, @required otherUserLatitude, @required otherUserLongitude}) {
double distanceInMeters = Geolocator.distanceBetween(myLatitude, myLongitude, otherUserLatitude, otherUserLongitude);
return distanceInMeters;
}
从数据库中获取经度和纬度后,您可以将它们传递给 getDistance
方法并在 Text
小部件中使用该值。
由于你的数据库结构没有包含在你的问题中,我用数字替换了获取坐标的实现。
将您的 ListView.builder
更新为:
return ListView.builder(
itemCount: snapshot.data.docs.length ,
itemBuilder: (context , int index){
DocumentSnapshot documentSnapshot = snapshot.data.docs [index];
// Replace the numbers with your implementation to get it from the database
final double latitude = 40.7128;
final double longitude = 74.0060;
final double otherUserLongitude = 51.5014;
final double otherUserLatitude = 0.1419;
final double distance = getDistance(myLatitude: myLatitude, myLongitude: myLongitude, otherUserLatitude: otherUserLatitude, otherUserLongitude: otherUserLongitude);
return Text(distance.toString());
}
);
除了维克多的回答
我只用一行就得到了其他简单的方法
Text((Geolocator.distanceBetween(41.0303668,28.9417857,42.0303668,29.9417857)/1000)toStringAsFixed(2))
感谢 dart 提供这种强大灵活的可能性
还要感谢 victor ,因为他的回答更广泛用于其他目的
飞镖
我将纬度和经度作为 `currentUser 。我在 firestore 中有文档每个用户文档都有自己的经度和纬度,
我还有一个方法可以得到两点之间的距离
getDistance(){
double distanceInMeters = Geolocator.distanceBetween(MyLongitude , myLatitude ,otherUserlatitude , otherUserLongiTude );
}
我也有这个简单的 Stream.BuilderWidget 和 listview.builder
StreamBuilder<QuerySnapshot>(stream: FirebaseFirestore.instance.collection(currentUser.uid).orderBy("timestamp",descending: true).snapshots(),
builder: (context , snapshot) {
if (!snapshot.hasData) {
return Center(
child: circulearProgress(),
);
}
return ListView.builder(
itemCount: snapshot.data.docs.length ,
itemBuilder: (context , int index){
DocumentSnapshot documentSnapshot = snapshot.data.docs [index] ;
return
// well i need to put the result of distance here after processing in the last method i mentioned (into Text.ToString)
);
}
)
首先处理成特定方法后,如何将数据提取到 ListView.Bulder
换句话说,我需要先在方法中处理数据,然后将结果提取到 listview.bulder
我不能处理这个因为我是初学者
或者一般来说,数据在listview.bulider或者Stream内部启动之前,如何处理成method。事不限远只
先谢谢各位朋友了
将 getDistance
方法更新为:
double getDistance({@required double myLatitude, @required double myLongitude, @required otherUserLatitude, @required otherUserLongitude}) {
double distanceInMeters = Geolocator.distanceBetween(myLatitude, myLongitude, otherUserLatitude, otherUserLongitude);
return distanceInMeters;
}
从数据库中获取经度和纬度后,您可以将它们传递给 getDistance
方法并在 Text
小部件中使用该值。
由于你的数据库结构没有包含在你的问题中,我用数字替换了获取坐标的实现。
将您的 ListView.builder
更新为:
return ListView.builder(
itemCount: snapshot.data.docs.length ,
itemBuilder: (context , int index){
DocumentSnapshot documentSnapshot = snapshot.data.docs [index];
// Replace the numbers with your implementation to get it from the database
final double latitude = 40.7128;
final double longitude = 74.0060;
final double otherUserLongitude = 51.5014;
final double otherUserLatitude = 0.1419;
final double distance = getDistance(myLatitude: myLatitude, myLongitude: myLongitude, otherUserLatitude: otherUserLatitude, otherUserLongitude: otherUserLongitude);
return Text(distance.toString());
}
);
除了维克多的回答 我只用一行就得到了其他简单的方法
Text((Geolocator.distanceBetween(41.0303668,28.9417857,42.0303668,29.9417857)/1000)toStringAsFixed(2))
感谢 dart 提供这种强大灵活的可能性 还要感谢 victor ,因为他的回答更广泛用于其他目的