gpt4 book ai didi

Flutter 使用 BloC 模式从 API 结果中获取列表元素

转载 作者:行者123 更新时间:2023-12-04 09:17:09 25 4
gpt4 key购买 nike

我是 BloC Pattern 的新手。我正在向天气 API 发送请求,它返回如下所示的 JSON 结果。
As you can see the result is array of json object
我在响应类中将其格式化为天气模型。
气象模型类

class Weather {
String date;
String day;
String icon;
String description;
String status;
String degree;
String min;
String max;
String night;
String humidity;

Weather({this.date, this.day, this.icon, this.description, this.status,
this.degree, this.min, this.max, this.night, this.humidity});

Weather.init(this.date, this.day, this.icon);

factory Weather.fromJson(Map<String, dynamic> json){
return Weather(
date: json['date'] as String,
day: json['day'] as String,
icon: json['icon'] as String,
description: json['description'] as String,
status: json['status'] as String,
degree: json['degree'] as String,
min: json['min'] as String,
max: json['max'] as String,
night: json['night'] as String,
humidity: json['humidity'] as String,
);
}
响应类
class WeatherResponse {
bool success;
String city;
List<Weather> result;

WeatherResponse({this.success, this.city, this.result});

factory WeatherResponse.fromJson(Map<String, dynamic> json) {
var weathersFromJson = json['result'] as List<dynamic>;
List<Weather> weatherList = List<Weather>();
weathersFromJson.forEach((element) {
Weather weather = Weather.fromJson(element);
weatherList.add(weather);
});

return WeatherResponse(
success: json['success'] as bool,
city: json['city'] as String,
result: weatherList
);

}
}
天气板块
enum WeatherEvent { getWeather, getDetails }

class WeatherBloc extends Bloc<WeatherEvent, Weather> {
WeatherBloc(Weather initialState) : super(initialState);

@override
Stream<Weather> mapEventToState(WeatherEvent event) async*{
switch (event) {
case WeatherEvent.getWeather:
WeatherRequest().fetchWeathers().then((weatherResponse) =>
weatherResponse.result.forEach((element) {
return element;
})
);
break;

case WeatherEvent.getDetails:
break;
}
}
}
我想使用 yield 而不是 return 元素,当数据到来时,我想将此数据天气列表结果元素发送到 UI。
UI Bloc Builder
 BlocBuilder<WeatherBloc, Weather>(
builder: (context, result) {
print(result.date);
return (Text(result.date));
},
),
也许我错过了一些东西。当我写 print(result.date);它打印到我的 Bloc 类。但是在 UI 类和 BlocBuilder 中什么都没有。当我调试它时,我看到:在返回元素行工作之后,它不会跳转到 BlocBuilder,因为每个都在继续。但是当它完成它仍然不跳。也许我的架构或方法可能是错误的。我也愿意接受关于那的建议。
所以总而言之,我无法在 bloc builder 中将“流结果的列表元素”一一作为流。任何人都可以帮忙吗?

最佳答案

您尚未为您的集团定义状态。
你应该创建一个状态

class WeatherState
class WeatherLoaded extends WeatherState{
final Weather weather;
WeatherLoaded(this.weather);
}
那么你的 Bloc 类应该看起来像这个 WeatherEvent 和 WeatherState
class WeatherBloc extends Bloc<WeatherEvent, WeatherState> {
WeatherBloc(Weather initialState) : super(initialState);

@override
Stream<Weather> mapEventToState(WeatherEvent event) async*{
switch (event) {
case WeatherEvent.getWeather:
// Api call call that returns a weather object.
final res = await WeatherRequest().fetchWeathers();
yield WeatherLoaded(res);
break;

case WeatherEvent.getDetails:
break;
}
}
}
接下来,您的 Bloc Builder 应如下所示
         BlocBuilder<WeatherBloc, WeatherState>(
builder: (context, WeatherState state) {
if(state is WeatherLoaded) {
return (Text(state.date));
}

},
),
您应该重写 API 调用以返回 Weather 对象

关于Flutter 使用 BloC 模式从 API 结果中获取列表元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63168025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com