0
votes

Error code

E/flutter (25456): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null. E/flutter (25456): Receiver: null E/flutter (25456): Tried calling: E/flutter (25456): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5) E/flutter (25456): #1
_WeatherState.fetchLocationUpDate. (package:tast_project/screenes/screenweather.dart:31:27) E/flutter (25456): #2 State.setState (package:flutter/src/widgets/framework.dart:1148:30) E/flutter (25456): #3 _WeatherState.fetchLocationUpDate (package:tast_project/screenes/screenweather.dart:30:5) E/flutter (25456): E/flutter (25456): #4
_WeatherState.initState (package:tast_project/screenes/screenweather.dart:42:5) E/flutter (25456): #5 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4355:58) E/flutter (25456): #6 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5) E/flutter (25456): #7 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456): #8 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456): #9 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14) E/flutter (25456): #10 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456): #11 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456): #12 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16) E/flutter (25456): #13 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5) E/flutter (25456): #14 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5) E/flutter (25456): #15 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5) E/flutter (25456): #16 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456): #17 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456): #18 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14) E/flutter (25456): #19 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456): #20 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456): #21 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14) E/flutter (25456): #22 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456): #23 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456): #24 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16) E/flutter (25456): #25 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5) E/flutter (25456): #26 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5) E/flutter (25456): #27 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11) E/flutter (25456): #28 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5) E/flutter (25456): #29 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456): #30 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456): #31 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14) E/flutter (25456): #32 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456): #33 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456): #34 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14) E/flutter (25456): #35 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456): #36 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456): #37 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16) E/flutter (25456): #38 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5) E/flutter (25456): #39 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5) E/flutter (25456): #40 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11) E/flutter (25456): #41 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5) E/flutter (25456): #42 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456): #43 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456): #44 ComponentElement.performRebuild (package:flutter/src/widgets/framework.da


   Flutter code weather

  

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    import 'dart:async';
    import 'package:http/http.dart' as http;
    import 'dart:convert';
    
    class Weather extends StatefulWidget {
      @override
      _WeatherState createState() => _WeatherState();
    }
    const ipkay = '14f4090b86e2548527113ccac56a5ae4';
    class _WeatherState extends State<Weather> {
      var temp = 0;
      String city='';
      //var id = 4099194;
      String location ;
      var textEditingController = TextEditingController();
      fetchSearch(String input) async {
          http.Response response = await http.get(
             "http://api.openweathermap.org/data/2.5/weather?units=metric&appid=14f4090b86e2548527113ccac56a5ae4&q=" + input);
          var result = json.decode(response.body);
           setState(() {
             location =result['name'];
           });}
    
      fetchLocationUpDate()async{
        http.Response response = await http.get(
         'http://api.openweathermap.org/data/2.5/weather?units=metric&appid=14f4090b86e2548527113ccac56a5ae4&q=$location');
        var result = json.decode(response.body);
        setState(() {
          temp =result['main']['temp'];
           city =result['name'];
        });}
    
      glo(String city){
        fetchSearch(city);
        fetchLocationUpDate();}
    
      @override
      void initState() {
        super.initState();
        fetchLocationUpDate();}
    
      @override
      Widget build(BuildContext context) {
        Size size = MediaQuery.of(context).size;
        return Scaffold(
          backgroundColor: Colors.transparent,
          body: SafeArea(
            child: Container(
              width: size.width,
              height: size.height,
              decoration: BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage(
                        'assetes/img/night.jpg',
                      ),
                      fit: BoxFit.fill)),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    '${temp.toString()} C°',
                    style: TextStyle(color: Colors.white, fontSize: 30),
                  ),
                  Text(
                    '$city',
                    style: TextStyle(color: Colors.white, fontSize: 30),
                  ),
                  Card(
                    child: TextField(
                      onSubmitted: (String city) {
                        glo(city);
                      },
                      controller: textEditingController,
                      decoration: InputDecoration(
                        hintText: 'please entre city',
                        prefixIcon: Icon(Icons.search),
                      ),
                    ),
                  ),
                )
                ],
              ),
            ),
          ),
        );
      }

  
1

1 Answers

0
votes

You can copy paste run full code below
Step 1: double temp = 0;
Step 2: glo need async and await

glo(String city) async {
    await fetchSearch(city);
    await fetchLocationUpDate();
  }

Step 3: Check city exist or not

if (result["cod"] == 200) {
      setState(() {
        location = result['name'];
        print("locaton $location");
      });
    }

working demo

enter image description here

full code

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';

class Weather extends StatefulWidget {
  @override
  _WeatherState createState() => _WeatherState();
}

const ipkay = '14f4090b86e2548527113ccac56a5ae4';

class _WeatherState extends State<Weather> {
  double temp = 0;
  //var id = 4099194;
  String location;
  var textEditingController = TextEditingController();
  fetchSearch(String input) async {
    http.Response response = await http.get(
        "http://api.openweathermap.org/data/2.5/weather?units=metric&appid=14f4090b86e2548527113ccac56a5ae4&q=" +
            input);
    var result = json.decode(response.body);
    print("fetchSearch ${response.body}");
    if (result["cod"] == 200) {
      setState(() {
        location = result['name'];
        print("locaton $location");
      });
    }
  }

  fetchLocationUpDate() async {
    print("location to string ${location.toString()}");
    http.Response response = await http.get(
        'http://api.openweathermap.org/data/2.5/weather?units=metric&appid=14f4090b86e2548527113ccac56a5ae4&q=' +
            location.toString());
    print("LocationUpdate ${response.body}");
    var result = json.decode(response.body);
    if (result["cod"] == 200) {
      setState(() {
        temp = result['main']['temp'];
      });
    }
  }

  glo(String city) async {
    await fetchSearch(city);
    await fetchLocationUpDate();
  }

  @override
  void initState() {
    super.initState();
    //fetchLocationUpDate();
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: SafeArea(
        child: Container(
          width: size.width,
          height: size.height,
          decoration: BoxDecoration(
              image: DecorationImage(
                  image: NetworkImage(
                    'https://picsum.photos/250?image=9',
                  ),
                  fit: BoxFit.fill)),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                '${temp.toDouble()} C°',
                style: TextStyle(color: Colors.white, fontSize: 30),
              ),
              Card(
                child: TextField(
                  onSubmitted: (String city) {
                    glo(city);
                  },
                  controller: textEditingController,
                  decoration: InputDecoration(
                    hintText: 'please entre city',
                    prefixIcon: Icon(Icons.search),
                  ),
                ),
              ),
              //              RaisedButton(
              //                onPressed: () {
              //                  glo();
              //                },
              //              )
            ],
          ),
        ),
      ),
    );
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: Weather(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}