简体   繁体   English

Flutter 肘不变 state

[英]Flutter cubit doesn't change state

I'm studying bloc in Flutter now and faced a problem.我现在正在 Flutter 学习 bloc 并遇到问题。

In my test app I fetch weather data and when trying to display it I see no changes and no errors.在我的测试应用程序中,我获取天气数据,在尝试显示它时,我没有看到任何变化,也没有错误。

The trouble is: I do get the data and can see with print, but when it comes to displaying with BlocBuilder, it seems to not change the state.问题是:我确实得到了数据并且可以通过打印看到,但是当使用 BlocBuilder 显示时,它似乎并没有改变 state。

This is the link to my repo - https://github.com/PhilippBekher/flutter_weather这是我的仓库的链接 - https://github.com/PhilippBekher/flutter_weather

The logics for displaying data are in main.dart The weather data is accessible via the link - https://api.weatherapi.com/v1/forecast.json?key=%20f148ee51b7cd4e2390e110556221408&q=London&days=1 The logics for displaying data are in main.dart The weather data is accessible via the link - https://api.weatherapi.com/v1/forecast.json?key=%20f148ee51b7cd4e2390e110556221408&q=London&days=1

When trying to print the state inside the file for the cubit, I can't see the emitted state (WeatherFetchLoaded): The screen below:当试图在文件中打印 state 时,我看不到发出的 state (WeatherFetchLoaded):屏幕如下:

I ran your code.我运行了你的代码。 Here is the outcome这是结果

It came in is catch block line.Here you are emitting no value.它进来的是catch block line。在这里你没有发出任何价值。 So only Its still in loading state.所以只有它还在加载 state。

 Future<void> fetchWeatherApi() async {
    emit(WeatherFetchLoading());

    try {
      final Weather weather = await apiRepository.getWeather();
      print('hello');

      emit(WeatherFetchLoaded(weather: weather));
    } on Failure catch (err) {
      emit(WeatherFetchError(failure: err));
    } catch (err) {
      print("Error 1: $err");  **// Here** 
    }
  }

It shows表明

'double' is not a subtype of type 'int' “double”不是“int”类型的子类型

在此处输入图像描述

This is a parsing issue while converting to the values to double.这是将值转换为双倍时的解析问题。 Your approach of parsing json is not a recommended way.您解析 json 的方法不是推荐的方法。 You can read here more about json parsing.您可以在此处阅读有关 json 解析的更多信息。 If you need to continue to debug,it may be time consuming to find exact field.如果您需要继续调试,找到确切的字段可能会很耗时。 You can try looking on each field.您可以尝试查看每个字段。 You need to make sure you are parsing correct data type.您需要确保解析正确的数据类型。 Model class and json should have same data type. Model class 和 json 应该具有相同的数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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