简体   繁体   中英

setState() called in constructor: _WeatherState#823a5(lifecycle state: created, no widget, not mounted) error flutter

weather api page

please help me I have a problem in code, I want to change myURL variable from mycity variable, I have a set of city how I can do that :

Editor Error : setState() called in constructor: _WeatherState#823a5(lifecycle state: created, no widget, not mounted)

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:marwa_app/component/myResponsiveLibrary.dart';
import 'package:marwa_app/component/Logo.dart';
import 'package:marwa_app/component/MyDrawer.dart';
import 'package:http/http.dart' as http;


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

}


class _WeatherState extends State<Weather> {
  List <String> mycity = ['aqaba','amman'];
  int i= 0;

  void cityValue(value) {
    setState(() {
      i = value;
    });
  }
  @override

  Future<Map> getData() async{
      String myURL = await 'http://api.openweathermap.org/data/2.5/forecast?q=${mycity[i]}&appid=41a6cd80a840ce00a3c77b8ba1a04022&units=metric';
      http.Response response = await http.get(myURL);
      return json.decode(response.body);
  }


  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: MainModel().mainColor,
        appBar: AppBar(
          backgroundColor: Color(0xff323266),
          leading: IconButton(
              icon: Icon(
                Icons.notifications_active,
                color: MainModel().thirdColor,
              ),
              onPressed: () => {}),
          title: Logo(),
          centerTitle: true,
          actions: [
            Builder(
              builder: (context) => IconButton(
                icon: Icon(
                  Icons.menu,
                  color: MainModel().thirdColor,
                ),
                onPressed: () => Scaffold.of(context).openEndDrawer(),
                tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
              ),
            ),
          ],
        ),
        endDrawer: Drawer(
          child: MyDrawer(),
        ),
        body: widgetMyData()
    );
  }
}
Widget widgetMyData() {

  return FutureBuilder(future: _WeatherState().getData(),
      builder: (BuildContext context, AsyncSnapshot<Map> snapshop){
        final widthScreen = MediaQuery.of(context).size.width;
        if(snapshop.hasData){
          Map content = snapshop.data;


          return Container(
            color: MainModel().secondColor,
            child: Center(
              child: ListView(
                  physics: NeverScrollableScrollPhysics(),
                  children: [
                Padding(padding: EdgeInsets.only(top: 20)),

                Container(
                  margin: EdgeInsets.symmetric(horizontal: MainModel().setPadding(MainModel().largePadding, widthScreen)),
                  height: 300,
                  decoration: BoxDecoration(
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black.withOpacity(0.15),
                          spreadRadius: 5,
                          blurRadius: 5,
                          offset: Offset(2, 2), // changes position of shadow
                        ),],
                      borderRadius: BorderRadius.all(Radius.circular(40)),
                      gradient: LinearGradient(
                          begin: Alignment.topLeft,
                          end: Alignment.bottomRight,
                          colors: [
                            MainModel().mainColor, MainModel().secondColor
                          ])
                  ),
                  child: Column(
                    children: [
                      Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().smallPadding, widthScreen)),),
                      Image.network('http://openweathermap.org/img/w/${content["list"][0]["weather"][0]["icon"]}.png',
                        color: MainModel().whiteColor.withOpacity(0.7),
                        fit: BoxFit.contain,
                        width: 120,),
                      Text('${content['list'][0]['weather'][0]['description']}',style: TextStyle(
                        fontSize: MainModel().setFont(MainModel().middleFont, widthScreen),
                        color: MainModel().whiteColor.withOpacity(0.7),
                      ),),
                      Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().largePadding * 1.5, widthScreen)),),
                      Text("${content['list'][0]['main']['temp']}\u00b0C",
                        style: TextStyle(
                            fontSize: MainModel().setFont(MainModel().largeFont * 1.5, widthScreen),
                            color: MainModel().whiteColor,
                            fontWeight: FontWeight.bold
                        ),),
                      Container(
                          alignment: Alignment.center,
                          child: Text("${content['city']['name']}",style: TextStyle(
                            fontSize: MainModel().setFont(MainModel().middleFont, widthScreen),
                            color: MainModel().whiteColor,
                          ),)
                      ),
                      Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().largePadding * 1.5, widthScreen)),),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Text("${content['list'][0]['main']['temp_max']}\u00b0C",style: TextStyle(
                              fontSize: MainModel().setFont(MainModel().middleFont, widthScreen),
                              color: MainModel().whiteColor,
                              fontWeight: FontWeight.bold
                          ),),
                          Text("${content['list'][0]['main']['temp_min']}\u00b0C",style: TextStyle(
                              fontSize: MainModel().setFont(MainModel().middleFont, widthScreen),
                              color: MainModel().whiteColor,
                              fontWeight: FontWeight.bold
                          ),),
                        ],
                      ),
                    ],
                  ),
                ),
                Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().largePadding * 2, widthScreen))),
                SingleChildScrollView(
                    scrollDirection: Axis.horizontal,
                    child: Row(
                      children: [
                        Padding(padding: EdgeInsets.only(left: MainModel().setPadding(MainModel().middlePadding, widthScreen))),
                        Container(
                            height: MainModel().setSize(MainModel().largeSize, widthScreen),
                            width: MainModel().setSize(MainModel().largeSize, widthScreen),
                            decoration: BoxDecoration(
                              color: Color(0xffFFB20F),
                              borderRadius: BorderRadius.all(Radius.circular(40.0)),
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().largePadding, widthScreen))),

                              ],
                            )
                        ),
                        Padding(padding: EdgeInsets.only(left: MainModel().setPadding(MainModel().middlePadding, widthScreen))),

                        Container(
                            height: MainModel().setSize(MainModel().largeSize, widthScreen),
                            width: MainModel().setSize(MainModel().largeSize, widthScreen),
                            decoration: BoxDecoration(
                              color: Color(0xff0DA6A6),
                              borderRadius: BorderRadius.all(Radius.circular(40.0)),
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().largePadding, widthScreen))),
                              ],
                            )
                        ),
                        Padding(padding: EdgeInsets.only(left: MainModel().setPadding(MainModel().middlePadding, widthScreen))),

                        Container(
                            height: MainModel().setSize(MainModel().largeSize, widthScreen),
                            width: MainModel().setSize(MainModel().largeSize, widthScreen),
                            decoration: BoxDecoration(
                              color: Color(0xff6D5DF1),
                              borderRadius: BorderRadius.all(Radius.circular(40.0)),
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().largePadding, widthScreen))),
                              ],
                            )
                        ),
                        Padding(padding: EdgeInsets.only(left: MainModel().setPadding(MainModel().middlePadding, widthScreen))),
                        FlatButton(onPressed: () {
                          _WeatherState().cityValue(1);
                        },
                            child: Container(
                                height: MainModel().setSize(MainModel().largeSize, widthScreen),
                                width: MainModel().setSize(MainModel().largeSize, widthScreen),
                                decoration: BoxDecoration(
                                  color: Color(0xffFF0F97),
                                  borderRadius: BorderRadius.all(Radius.circular(40.0)),
                                ),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    Text('click'),
                                    Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().largePadding, widthScreen))),
                                  ],
                                )
                            ),
                        ),
                        Padding(padding: EdgeInsets.only(left: MainModel().setPadding(MainModel().middlePadding, widthScreen))),

                      ],
                    ))
              ]),
            ),
          );
        }else{
          return Container(
            child: Text('sorry you data is not found'),
          );
        }
      }
  );
}

if click in _WeatherState().cityValue(1); change api city name

You can copy paste run full code below
You can pass getData() and cityValue to widgetMyData(Future<Map> _future, Function callback)
code snippet

  Future<Map> _future;
  ...
  @override
  initState() {
    _future = getData();
    super.initState();
  }
  ...
  body: widgetMyData(_future, cityValue));
  
  ...
  Widget widgetMyData(Future<Map> _future, Function callback) {
     return FutureBuilder(
       future: _future,
       ...
       FlatButton(
            onPressed: () {
              callback(1);
            },
            child: Text('click'))

working demo

在此处输入图片说明

full code

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

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

class _WeatherState extends State<Weather> {
  List<String> mycity = ['aqaba', 'amman'];
  int i = 0;
  Future<Map> _future;

  void cityValue(value) {
    setState(() {
      i = value;
      _future = getData();
    });
  }

  @override
  initState() {
    _future = getData();
    super.initState();
  }

  Future<Map> getData() async {
    String myURL =
        await 'https://api.openweathermap.org/data/2.5/forecast?q=${mycity[i]}&appid=41a6cd80a840ce00a3c77b8ba1a04022&units=metric';
    http.Response response = await http.get(myURL);
    return json.decode(response.body);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        //backgroundColor: MainModel().mainColor,
        appBar: AppBar(
          backgroundColor: Color(0xff323266),
          leading: IconButton(
              icon: Icon(
                Icons.notifications_active,
                //color: MainModel().thirdColor,
              ),
              onPressed: () => {}),
          title: Text("Logo()"),
          centerTitle: true,
          actions: [
            Builder(
              builder: (context) => IconButton(
                icon: Icon(
                  Icons.menu,
                  //color: MainModel().thirdColor,
                ),
                onPressed: () => Scaffold.of(context).openEndDrawer(),
                tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
              ),
            ),
          ],
        ),
        body: widgetMyData(_future, cityValue));
  }
}

Widget widgetMyData(Future<Map> _future, Function callback) {
  return FutureBuilder(
      future: _future,
      builder: (BuildContext context, AsyncSnapshot<Map> snapshop) {
        final widthScreen = MediaQuery.of(context).size.width;
        if (snapshop.hasData) {
          Map content = snapshop.data;

          return Container(
            child: Center(
              child:
                  ListView(physics: NeverScrollableScrollPhysics(), children: [
                Padding(padding: EdgeInsets.only(top: 20)),
                Container(
                  height: 300,
                  decoration: BoxDecoration(
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black.withOpacity(0.15),
                          spreadRadius: 5,
                          blurRadius: 5,
                          offset: Offset(2, 2), // changes position of shadow
                        ),
                      ],
                      borderRadius: BorderRadius.all(Radius.circular(40)),
                      gradient: LinearGradient(
                          begin: Alignment.topLeft,
                          end: Alignment.bottomRight,
                          colors: [Colors.blue, Colors.red])),
                  child: Column(
                    children: [
                      Image.network(
                        'https://openweathermap.org/img/w/${content["list"][0]["weather"][0]["icon"]}.png',
                        //color: MainModel().whiteColor.withOpacity(0.7),
                        fit: BoxFit.contain,
                        width: 120,
                      ),
                      Text(
                        '${content['list'][0]['weather'][0]['description']}',
                      ),
                      Text(
                        "${content['list'][0]['main']['temp']}\u00b0C",
                      ),
                      Container(
                          alignment: Alignment.center,
                          child: Text(
                            "${content['city']['name']}",
                          )),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Text(
                            "${content['list'][0]['main']['temp_max']}\u00b0C",
                          ),
                          Text(
                            "${content['list'][0]['main']['temp_min']}\u00b0C",
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                SingleChildScrollView(
                    scrollDirection: Axis.horizontal,
                    child: Row(
                      children: [
                        FlatButton(
                            onPressed: () {
                              callback(1);
                            },
                            child: Text('click')),
                      ],
                    ))
              ]),
            ),
          );
        } else {
          return Container(
            child: Text('sorry you data is not found'),
          );
        }
      });
}

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),
      ),
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM