【问题标题】:Flutter how to migrate BLoC old version code to Bloc 8.0.1 versionFlutter 如何将 BLoC 旧版本代码迁移到 Bloc 8.0.1 版本
【发布时间】: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


    【解决方案1】:

    这应该可行。

    您还需要将 Flutter bloc 依赖项更改为最新的才能完成这项工作。

    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:flutter_bloc/flutter_bloc.dart';
    import 'package:geolocator/geolocator.dart';
    
    class WeatherBloc extends Bloc<WeatherEvent, WeatherState> {
      WeatherBloc() : super(WeatherInitial()) {
        on<WeatherRequested>((event, emit) async {
          emit(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);
            emit(
                WeatherLoadSuccess(weather: weather, hourlyWeather: hourlyWeather));
          } catch (e) {
            emit(WeatherLoadFailure());
          }
        });
    
        on<WeatherCurrentPositionRequested>((event, emit) async {
          LocationPermission permission = await Geolocator.checkPermission();
          if (permission == LocationPermission.whileInUse ||
              permission == LocationPermission.always) {
            Position lastKnownPosition = await Geolocator.getLastKnownPosition();
            if (lastKnownPosition != null) {
              add(WeatherRequested(
                  lat: lastKnownPosition.latitude.toString(),
                  lon: lastKnownPosition.longitude.toString()));
            } else {
              Position position = await Geolocator.getCurrentPosition(
                  desiredAccuracy: LocationAccuracy.high);
              add(WeatherRequested(
                  lat: position.latitude.toString(),
                  lon: position.longitude.toString()));
            }
          } else {
            await Geolocator.requestPermission();
            add(WeatherCurrentPositionRequested());
          }
        });
      }
    }
    

    bloc 提供者需要以下签名才能工作。

    BlocProvider(
      create: (context) => WeatherBloc()
        ..add(
          WeatherCurrentPositionRequested(),
        ),
      child: ... your code
    ),
    

    【讨论】:

      猜你喜欢
      • 2022-08-12
      • 2022-06-28
      • 1970-01-01
      • 2022-07-10
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      相关资源
      最近更新 更多