【发布时间】:2022-01-10 08:10:58
【问题描述】:
在研究 Bloc 从旧版本迁移到新 8 时。我无法修复转换到新版本。我遇到了许多我没有弄清楚的未定义的事情。
import 'dart:async';
import 'package:WeatherApp/events/WeatherEvent.dart';
import 'package:WeatherApp/models/Weather.dart';
import 'package:WeatherApp/services/WeatherService.dart';
import 'package:WeatherApp/states/WeatherState.dart';
import 'package:bloc/bloc.dart';
import 'package:geolocator/geolocator.dart';
class WeatherBloc extends Bloc<WeatherEvent, WeatherState> {
WeatherBloc() : super(null) {
add(WeatherCurrentPositionRequested());
}
@override
Stream<WeatherState> mapEventToState(WeatherEvent event) async* {
if (event is WeatherRequested) {
yield* _newWeatherRequested(event);
}
if (event is WeatherCurrentPositionRequested) {
yield* _newWeatherCurrentPositionRequested();
}
}
Stream<WeatherState> _newWeatherRequested(WeatherRequested event) async* {
yield WeatherLoadInProgress();
try {
final Weather weather = await WeatherService.fetchCurrentWeather(
query: event.city, lon: event.lon, lat: event.lat);
final List<Weather> hourlyWeather =
await WeatherService.fetchHourlyWeather(
query: event.city, lon: event.lon, lat: event.lat);
yield WeatherLoadSuccess(weather: weather, hourlyWeather: hourlyWeather);
} catch (e) {
yield WeatherLoadFailure();
}
}
Stream<WeatherState> _newWeatherCurrentPositionRequested() async* {
LocationPermission permission = await checkPermission();
if (permission == LocationPermission.whileInUse ||
permission == LocationPermission.always) {
Position lastKnownPosition = await getLastKnownPosition();
if(lastKnownPosition != null) {
add(WeatherRequested(
lat: lastKnownPosition.latitude.toString(),
lon: lastKnownPosition.longitude.toString()));
} else {
Position position =
await getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
add(WeatherRequested(
lat: position.latitude.toString(),
lon: position.longitude.toString()));
}
} else {
await requestPermission();
add(WeatherCurrentPositionRequested());
}
}
}
https://github.com/evgenusov/FlutterWeatherApp/blob/master/lib/bloc/WeatherBloc.dart
【问题讨论】:
标签: flutter migration bloc flutter-bloc