【问题标题】:UI not updating in flutter after pop弹出后UI未更新
【发布时间】:2022-11-13 03:22:19
【问题描述】:

你好,希望你们一切都好。谈到这一点,我正在开发一个天气应用程序,它工作正常,但是当涉及到按城市获取天气时,它实际上不起作用它获取所有数据但不更新 UI 中的数据。

更新用户界面

void updateUI(var weatherData) {
   if (weatherData == null) {
     city = '';
     temperature = 0;
     weatherMessage = 'Unable to fetch data';
   }
   id = weatherData['weather'][0]['id'];
   weatherIcon = weatherModel.getWeatherIcon(id);
   city = weatherData['name'];
   double temp = weatherData['main']['temp'];
   temperature = temp.toInt();
   weatherMessage = weatherModel.getMessage(temperature);
   description = weatherData['weather'][0]['description'];
 }

接收城市名称(这是我猜的实际问题)

FlatButton(
                    onPressed: () async {
                      dynamic typedname = await Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => Cityscreen()));

                      setState(
                        () async {
                          dynamic weatherData =
                              await weatherModel.getCityLocation(typedname);
                          updateUI(weatherData);
                        },
                      );
                    },

获取城市位置

Future<dynamic> getCityLocation(String cityname) async
  {
    Network_helper network_helper=Network_helper('https://api.openweathermap.org/data/2.5/weather?q=$cityname&appid=$key');
    var weatherData=await network_helper.getData();
    return weatherData;
  }

城市屏幕有状态小部件

class _CityscreenState extends State<Cityscreen> {
  late String cityName;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('images/back1.jpg'),
            fit: BoxFit.fill,
          ),
        ),
        constraints: BoxConstraints.expand(),
        child: SafeArea(
          child: Column(
            children: [
              Align(
                alignment: Alignment.topLeft,
                child: FlatButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Icon(
                    Icons.arrow_back_ios,
                    size: 50.0,
                  ),
                ),
              ),
              Container(
                padding: EdgeInsets.all(20.0),
                child: TextField(
                  style: TextStyle(
                    color: Colors.black,
                  ),
                  decoration: kTextFieldDecoration,
                  onChanged: (value)
                  {
                    cityName=value;
                  },
                ),

              ),
              FlatButton(
                  onPressed: (){
                    Navigator.pop(context,cityName);
                  },
                child: Text(
                  'Get weather',
                  style: TextStyle(
                    fontSize: 30,
                  ),
                ),
                color: Colors.deepPurpleAccent,
              ),
            ],
          ),
        ),
      ),

提前致谢。

【问题讨论】:

  • 我没有看到 _CityscreenState 内的函数 updateUI 被触发,我认为它在 pop 将让你进入的小部件中,我看到你需要在 _CityscreenState 内执行回调,这将触发 updateUI在另一个小部件中,或使用状态管理解决方案来全局更新状态。
  • 那是因为城市屏幕将保持不变,它只会通过返回一个基本上是城市名称的字符串弹出,并且传递城市名称以获取城市位置,然后简单地调用 updateUI 我已经尝试了所有打印一切都很好但唯一的问题是我的用户界面没有更新
  • 请在获取弹出值的位置添加代码。
  • 首先看一下城市屏幕(城市屏幕有状态小部件),它在平面按钮中弹出值,看看接收城市名称,它接收该字符串并将其存储在类型名称中
  • Recieving city name 中,如果值typedname 即将到来,是否在setState 之前打印?

标签: flutter dart flutter-layout dart-pub


【解决方案1】:

在中使用 setstate然后部分在导航推送代码。

Navigator.push(context,MaterialPageRoute(
                              builder: (context) => Cityscreen())).then((value) {
  setState(() async {
    dynamic weatherData =
                              await weatherModel.getCityLocation(typedname);
                          updateUI(weatherData);
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2020-11-06
    • 1970-01-01
    • 2019-04-27
    • 2017-04-16
    相关资源
    最近更新 更多