I have a provider where a method , by this method if I send lat and long it will give me place name.
Future<List<Placemark>> getAndSetAddressFromLatLong(double startLat)async {
List<Placemark> placemarksStart = await placemarkFromCoordinates(startLat,startLong);
return placemarksStart;
}
So, When I'm trying to call and fetch the data in view file like below
@override
Widget build(BuildContext context) {
var data = Provider.of<MapProvider>(context).getAndSetAddressFromLatLong(
widget.history.startLat!.toDouble(),
widget.history.startLong!.toDouble(),
).then((value) => value);
print(data);
I'm getting the output I/flutter (25255): Instance of 'Future<List<Placemark>>'
, But In then() if I print value without return I'm getting my desire list.
How I will get List<Placemark>
here from Instance of 'Future<List>' ?
CodePudding user response:
late List<placemark> data;
@override
Widget build(BuildContext context) {
Provider.of<MapProvider>(context).getAndSetAddressFromLatLong(
widget.history.startLat!.toDouble(),
widget.history.startLong!.toDouble(),
).then((value){
data = value;
print(data);
}
);
CodePudding user response:
Since you're using provider call notifyListeners()
after awaiting the results. In the widget use consumer to show the results
List<Placemark> _placemarksStart = [];
List<Placemark> get placemarksStart => [..._placemarksStart];
Future<void> getAndSetAddressFromLatLong(double startLat, double startLong) async {
_placemarksStart = await placemarkFromCoordinates(startLat,startLong);
notifyListeners();
}
Widget, similarly you can achieve loading with a boolean
Consumer<MyType>(
builder: (context, provider, child) {
if (provider.placemarksStart.isEmpty) {
return Center(child: Text('Loading...'),);
}
return ListView.builder(itemBuilder: (context, index) {
final item = provider.placemarksStart[index];
return Text("TODO");
}, itemCount: provider.placemarksStart.length,);
},
),
And call the method getAndSetAddressFromLatLong
in the initState