Flutter Bloc 和 Http Library bloc 状态未更新
Flutter Bloc and Http Library bloc state not updating
我使用 bloc 8.0.1
和 http ^0.13.4
。
当您 运行 应用程序时,屏幕上只会显示 WeatherInitial()
状态。我看不到其他州。 debugPrint("debug3");
我在控制台中看不到这行代码。 try catch 不在代码行内。你能找出问题所在吗?
集团
class WeatherBloc extends Bloc<WeatherEvent, WeatherState> {
final WeatherRepository repository = locator<WeatherRepository>();
WeatherBloc() : super(WeatherInitial()) {
on<WeatherEvent>((event, emit) async {
if (event is FetchWeatherEvent) {
WeatherLoadingState();
try {
final Weather? weather = await repository.getWeather(event.cityName);
WeatherLoadedState(weather: weather!);
debugPrint("debug3");
} catch (_) {
WeatherErrorState();
}
}
});
}
}
事件
abstract class WeatherEvent extends Equatable {
const WeatherEvent();
@override
List<Object> get props => [];
}
class FetchWeatherEvent extends WeatherEvent {
final String cityName;
const FetchWeatherEvent({required this.cityName});
}
州
abstract class WeatherState extends Equatable {
const WeatherState();
@override
List<Object> get props => [];
}
class WeatherInitial extends WeatherState {}
class WeatherLoadingState extends WeatherState {}
class WeatherLoadedState extends WeatherState {
final Weather weather;
const WeatherLoadedState({required this.weather});
}
class WeatherErrorState extends WeatherState {}
正文
class WeatherApp extends StatelessWidget {
String? selectedCity;
@override
Widget build(BuildContext context) {
final _weatherBloc = BlocProvider.of<WeatherBloc>(context);
return Scaffold(
appBar: AppBar(
title: const Text("WeatherApp"),
actions: [
IconButton(
onPressed: () async {
selectedCity = await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const CityChoose(),
),
);
if (selectedCity != null) {
_weatherBloc.add(FetchWeatherEvent(cityName:
selectedCity!));
}
},
icon: const Icon(Icons.search),
),
],
),
body: Center(
child: BlocBuilder(
bloc: _weatherBloc,
builder: (context, WeatherState state) {
if (state is WeatherInitial) {
return const Center(
child: Text("Select City"),
);
}
if (state is WeatherLoadingState) {
return const Center(
child: CircularProgressIndicator(),
);
}
if (state is WeatherLoadedState) {
final weather = state.weather;
debugPrint(weather.title);
return ListView(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: LocationWidget(
selectedCity: weather.title,
)),
),
const Padding(
padding: EdgeInsets.all(8.0),
child: Center(child: LastUpdateWidget()),
),
const Padding(
padding: EdgeInsets.all(8.0),
child: Center(child: WeatherImageWiddget()),
),
const Padding(
padding: EdgeInsets.all(16.0),
child: Center(child: TemperatureCondition()),
),
],
);
} else {
return const Center(
child: Text("Error"),
);
}
},
),
),
您需要使用 emit
在 Bloc
中发出一个状态。 emit
在 on<T>
.
的闭包中声明
emit(WeatherLoadedState(weather: weather!));
emit(WeatherErrorState());
我使用 bloc 8.0.1
和 http ^0.13.4
。
当您 运行 应用程序时,屏幕上只会显示 WeatherInitial()
状态。我看不到其他州。 debugPrint("debug3");
我在控制台中看不到这行代码。 try catch 不在代码行内。你能找出问题所在吗?
集团
class WeatherBloc extends Bloc<WeatherEvent, WeatherState> {
final WeatherRepository repository = locator<WeatherRepository>();
WeatherBloc() : super(WeatherInitial()) {
on<WeatherEvent>((event, emit) async {
if (event is FetchWeatherEvent) {
WeatherLoadingState();
try {
final Weather? weather = await repository.getWeather(event.cityName);
WeatherLoadedState(weather: weather!);
debugPrint("debug3");
} catch (_) {
WeatherErrorState();
}
}
});
}
}
事件
abstract class WeatherEvent extends Equatable {
const WeatherEvent();
@override
List<Object> get props => [];
}
class FetchWeatherEvent extends WeatherEvent {
final String cityName;
const FetchWeatherEvent({required this.cityName});
}
州
abstract class WeatherState extends Equatable {
const WeatherState();
@override
List<Object> get props => [];
}
class WeatherInitial extends WeatherState {}
class WeatherLoadingState extends WeatherState {}
class WeatherLoadedState extends WeatherState {
final Weather weather;
const WeatherLoadedState({required this.weather});
}
class WeatherErrorState extends WeatherState {}
正文
class WeatherApp extends StatelessWidget {
String? selectedCity;
@override
Widget build(BuildContext context) {
final _weatherBloc = BlocProvider.of<WeatherBloc>(context);
return Scaffold(
appBar: AppBar(
title: const Text("WeatherApp"),
actions: [
IconButton(
onPressed: () async {
selectedCity = await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const CityChoose(),
),
);
if (selectedCity != null) {
_weatherBloc.add(FetchWeatherEvent(cityName:
selectedCity!));
}
},
icon: const Icon(Icons.search),
),
],
),
body: Center(
child: BlocBuilder(
bloc: _weatherBloc,
builder: (context, WeatherState state) {
if (state is WeatherInitial) {
return const Center(
child: Text("Select City"),
);
}
if (state is WeatherLoadingState) {
return const Center(
child: CircularProgressIndicator(),
);
}
if (state is WeatherLoadedState) {
final weather = state.weather;
debugPrint(weather.title);
return ListView(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: LocationWidget(
selectedCity: weather.title,
)),
),
const Padding(
padding: EdgeInsets.all(8.0),
child: Center(child: LastUpdateWidget()),
),
const Padding(
padding: EdgeInsets.all(8.0),
child: Center(child: WeatherImageWiddget()),
),
const Padding(
padding: EdgeInsets.all(16.0),
child: Center(child: TemperatureCondition()),
),
],
);
} else {
return const Center(
child: Text("Error"),
);
}
},
),
),
您需要使用 emit
在 Bloc
中发出一个状态。 emit
在 on<T>
.
emit(WeatherLoadedState(weather: weather!));
emit(WeatherErrorState());