0

I want to show weather name on dialog box ,But when i Click first time on button data is not loading only show dialog box after that when i click second time then my data is loading.

I want to show weather name on dialog box ,But when i Click first time on button data is not loading only show dialog box after that when i click second time then my data is loading.

I want to show my data on first click please help how to do this.

This is my code

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:weather/weather.dart';


void main() => runApp(MyAppp());



class MyAppp extends StatefulWidget {
  const MyAppp({ Key? key }) : super(key: key);

  @override
  _MyApppState createState() => _MyApppState();
}

class _MyApppState extends State<MyAppp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home:ButtonPage()
      
    );
  }
}

class ButtonPage extends StatefulWidget {
  const ButtonPage({ Key? key }) : super(key: key);

  @override
  _ButtonPageState createState() => _ButtonPageState();
}

class _ButtonPageState extends State<ButtonPage> {

    // passing this to latitude and longitude strings
     String key = '856822fd8e22db5e1ba48c0e7d69844a';
  late WeatherFactory ws;
  List<Weather> _data = [];
  double? lat, lon;

  @override
  void initState() {
    super.initState();
    ws = new WeatherFactory(key);
    
  }



void getCurrentLocation() async {

    var position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    var lat = position.latitude;
    var long = position.longitude;


  print(lat);
  print(long);
queryWeather( lat,long) ;
  }


 queryWeather(var lat,var long) async {
       print("Data: "+_data.toString());
    /// Removes keyboard
    FocusScope.of(context).requestFocus(FocusNode());

  
    Weather weather = await ws.currentWeatherByLocation(lat, long);
    setState(() {
      _data = [weather];
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: RaisedButton(onPressed: () { 
getCurrentLocation();
showDialog<void>(
    context: context,
    // barrierDismissible: false, // user must tap button!
    builder: (BuildContext context) {
      return Dialog(
        child: Center(
        child: ListView.builder(
          itemCount: _data.length,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text("${_data[index].areaName}"),
            );
          },
         
        ),
          ),
      );
    },
  );

       },
      child: Text("Button"),),),
      
    );
  }
}

I am using this permissions

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
weather: ^2.0.1
  geolocator: ^7.4.0
  url_launcher: ^6.0.9
Deepak
  • 1,664
  • 3
  • 19
  • 52

1 Answers1

1

Since getCurrentLocation is async, you have to mark your onPressed as async and await the result of getCurrentLocation:

Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: RaisedButton(onPressed: () async { 
         await getCurrentLocation();
         ...

Also you have to await queryWeather call as well:

void getCurrentLocation() async {

    var position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    var lat = position.latitude;
    var long = position.longitude;

    await queryWeather( lat,long) ;
  }
Peter Koltai
  • 8,296
  • 2
  • 10
  • 20