I receive the following NoSuchMethodError in my code:
I/flutter (22695): {nom: CLINIQUE CENTRE, latitude: 37.4259071, longitude: -122.1095606, distance: 0}
I/flutter (22695): latitude de : CLINIQUE CENTRE37.4259071
I/flutter (22695): {nom: CLINIQUE CHATEAU, latitude: 37.4420794, longitude: -122.1432758, distance: 0}
I/flutter (22695): latitude de : CLINIQUE CENTRE37.4259071
I/flutter (22695): NoSuchMethodError: Class 'double' has no instance method 'sort'.
I/flutter (22695): Receiver: 2.298635770797528
I/flutter (22695): Tried calling: sort(Closure: (dynamic, dynamic) => dynamic)
I want de get distance wetween two locations
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:flutter_sorting_location/Utils.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:flutter_sorting_location/Destination.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
double? distance;
List destinations = [];
Position? _currentPosition;
List<Destination> destinationlist = [];
Future<List> getData() async {
var url = 'http://aidemens.global-aeit.com/flutter/getlocation.php';
print(url);
final response = await http.get(Uri.parse(url));
// print(json.decode(response.body));
destinations = json.decode(response.body);
return json.decode(response.body);
}
@override
void initState() {
_getCurrentLocation();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Location sorting from current location"),
),
body: FutureBuilder<List>(
future: getData(),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error);
return snapshot.hasData
? ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
// print(snapshot.data!);
return Card(
margin: EdgeInsets.all(5),
elevation: 5,
child: Padding(
padding: EdgeInsets.all(5),
child: Container(
height: 40,
color: Colors.white,
child: Column(
children: [
Text("${snapshot.data![index]['nom']}"),
Text(
"${double.parse(snapshot.data![index]['distance']!).toStringAsFixed(2)} km"),
],
),
),
),
);
})
: const Center(
child: Text(
"Aucun encaissement ce jour",
style: TextStyle(color: Colors.red),
),
);
}));
}
// get Current Location
_getCurrentLocation() {
Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.best,
forceAndroidLocationManager: true)
.then((Position position) {
distanceCalculation(position);
setState(() {
_currentPosition = position;
});
}).catchError((e) {
print(e);
});
}
List tmp = [];
distanceCalculation(Position position) {
tmp.clear();
for (var d in destinations) {
if (destinations.isNotEmpty) {
tmp.add(d);
}
destinations = tmp;
print(d);
// for (var i = 0; i < destinations.length; i ) {
// print("latitude : " destinations[d]['latitude'].toDouble());
var km = getDistanceFromLatLonInKm(
position.latitude,
position.longitude,
double.parse(destinations[0]['latitude']),
double.parse(destinations[0]['longitude']));
// var m = Geolocator.distanceBetween(position.latitude,position.longitude, d.lat,d.lng);
// d.distance = m/1000;
destinations[0]['distance'] = km;
// d.distance = km;
//print("distance : ${d.distance}");
// print("distance : ${destinations[0]['distance']}");
//destinations.add(d);
//print(d);
// print("distance 2 : ${destinations[0]['distance']}");
/*
print(getDistanceFromLatLonInKm(
position.latitude,
position.longitude,
destinations[0]['latitude'].toDouble(),
destinations[0]['longitude'].toDouble()));
*/
print("latitude de : "
destinations[0]['nom']
destinations[0]['latitude']);
}
setState(() {
destinations[0]['distance'].sort((a, b) {
return a.double.parse(destinations[0]['distance'])
.compareTo(b.double.parse(destinations[0]['distance']) as double);
});
});
}
}
Utils.dart
import'dart:math' as Math;
//HaverSine formula
double getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2)
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}
double deg2rad(deg) {
return deg * (Math.pi/180);
}
my dbb
CREATE TABLE `adherent` (
`id` int(11) NOT NULL,
`nom` varchar(100) NOT NULL,
`latitude` double NOT NULL,
`longitude` double NOT NULL,
`distance` double NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Déchargement des données de la table `adherent`
--
INSERT INTO `adherent` (`id`, `nom`, `latitude`, `longitude`, `distance`) VALUES
(25, 'wwwwwwwwwwww', 37.4259071, -122.1095606, 0),
(35, 'xxxxxxxxxxx', 37.4420794, -122.1432758, 0);
code php
$db_datas = array();
$sql12 = "SELECT A.nom,A.latitude,A.longitude,A.distance
FROM adherent A
WHERE A.id IN(25,35)
";
$results = $conn->query($sql12);
if($results->num_rows >0){
while($row = $results->fetch_assoc()){
$db_datas[] = $row;
}//fin while
//Retourn toutes les reponses en json
echo json_encode(utf8ize($db_datas));
}else{
echo "erreur";
}
CodePudding user response:
this
destinations[0]['distance']
returns
double
so the error is correct here
setState(() {
destinations[0]['distance'].sort((a, b) {
return a.double.parse(destinations[0]['distance'])
.compareTo(b.double.parse(destinations[0]['distance']) as double);
});
});
CodePudding user response:
Here you added a double value and trying to sort it
destinations[0]['distance'].sort((a, b) {
return a.double.parse(destinations[0]['distance'])
.compareTo(b.double.parse(destinations[0]['distance']) as double);
});
destinations[0]['distance'] is a double value.. You should be sorting destinations like
destinations.sort((a, b) {
return (double.parse(a['distance'])).compareTo(double.parse(b['distance']));
});
CodePudding user response:
now
I/flutter (24786): http://xxxxxxxxxxx/flutter/getlocation.php I/flutter (24786): {nom: CLINIQUE CENTRE, latitude: 37.4259071, longitude: -122.1095606, distance: 0} I/flutter (24786): latitude de : CLINIQUE CENTRE37.4259071 I/flutter (24786): {nom: CLINIQUE CHATEAU, latitude: 37.4420794, longitude: -122.1432758, distance: 0} I/flutter (24786): latitude de : CLINIQUE CENTRE37.4259071 I/flutter (24786): type 'String' is not a subtype of type 'num' of 'other'
List tmp = [];
distanceCalculation(Position position) {
tmp.clear();
for (var d in destinations) {
if (destinations.isNotEmpty) {
tmp.add(d);
}
destinations = tmp;
print(d);
// for (var i = 0; i < destinations.length; i ) {
// print("latitude : " destinations[d]['latitude'].toDouble());
var km = getDistanceFromLatLonInKm(
position.latitude,
position.longitude,
double.parse(destinations[0]['latitude']),
double.parse(destinations[0]['longitude']));
// var m = Geolocator.distanceBetween(position.latitude,position.longitude, d.lat,d.lng);
// d.distance = m/1000;
destinations[0]['distance'] = km;
// d.distance = km;
//print("distance : ${d.distance}");
// print("distance : ${destinations[0]['distance']}");
//destinations.add(d);
//print(d);
// print("distance 2 : ${destinations[0]['distance']}");
/*
print(getDistanceFromLatLonInKm(
position.latitude,
position.longitude,
destinations[0]['latitude'].toDouble(),
destinations[0]['longitude'].toDouble()));
*/
print("latitude de : "
destinations[0]['nom']
destinations[0]['latitude']);
}
setState(() {
destinations.sort((a, b) {
return a['distance'].compareTo(b['distance']);
});
});
}
CodePudding user response:
error now
type 'double' is not a subtype of type 'String'
setState(() {
destinations.sort((a, b) {
return double.parse(a['distance'])
.compareTo(double.parse(b['distance']));
});
});