我的 flutter 應用程式有問題,我正在嘗試使用一個變數來存盤市場名稱,但是當我使用這個變數時,在我的回圈之外,它什么也沒顯示,我不知道為什么
var markets = [
["Marjane", "marjane-offers", 34.0590237712, -6.80169582367],
["Auto Hall", "autohall-offers", 34.0636, -6.7973],
["Bricoma", "bricoma-offers", 34.0706, -6.7883],
];
for (var i = 0; i < markets.length; i ) {
// GEOLOCATOR PACK
double stopLatitude = double.parse("${markets[i][2]}");
double stopLongitude = double.parse("${markets[i][3]}");
double distanceInMeters = Geolocator.distanceBetween(startLatitude, startLongitude, stopLatitude, stopLongitude);
if (distanceInMeters < 1000) {
var nameMarket = markets[i][0];
}
}
return Text('Location always change: \n${data.latitude}/${data.longitude} \n\n A9RAB MAHAL LIK: $nameMarket');
(此代碼檢查我是否靠近商店并向我顯示他的名字!)
uj5u.com熱心網友回復:
我假設您指的是變數nameMarket。在 return 陳述句時,變數不在范圍內。您可以做的是像String? nameMarket在 for 回圈之前一樣宣告變數(這將在 return 陳述句的范圍內)。然后分配給它像nameMarket = markets[i][0];
實際上,如果您只想要第一個足夠接近的商店而不檢查剩余的商店,則最好將 return 陳述句移動到 if 陳述句中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/382760.html
下一篇:如何將函式結果寫入實體變數
