我目前正在开发一个简单的项目 crypto_wallet。状态管理 (BLoC) 和价值平等 (冻结) 为 DB (Firebase) 创建 CRUD 操作,在 watch 方法中我使用 StreamSubcription 代码是:
@injectable
class CoinWatcherBloc extends Bloc {
final ICoinRepository _repository;
CoinWatcherBloc(this._repository, this._coinStreamSubscription) :
super(CoinWatcherState.initial());
StreamSubscription<Either<CoinFailure, KtList>>? _coinStreamSubscription;
@override
Stream mapEventToState(CoinWatcherEvent event) async* {
yield* event.map(
watchCoin: (e) async* {
yield CoinWatcherState.loadInProgress();
await _coinStreamSubscription?.cancel();
_coinStreamSubscription = _repository.watchCoin().listen(
(failureOrSuccess) => add(
CoinWatcherEvent.coinsReceived(failureOrSuccess),
),
);
},
coinsReceived: (e) async* {
yield e.failureOrCoin.fold(
(f) => CoinWatcherState.loadFailure(f),
(coin) => CoinWatcherState.loadSuccess(coin),
);
},
);
}
@override
Future close() async {
await _coinStreamSubscription?.cancel();
return super.close();
}
}
最后我关闭了流。我在@lazySingleton 上注入所有第 3 方模块:
@module
abstract class FirebaseInjectableModule {
@lazySingleton
FirebaseAuth get firebaseAuth => FirebaseAuth.instance;
@lazySingleton
FirebaseFirestore get firebaseFirestore => FirebaseFirestore.instance;
@lazySingleton
GoogleSignIn get googleSignIn => GoogleSignIn();
}
然后它说:
Object/factory with type StreamSubscription<Either<CoinFailure, KtList>> is not
registered inside GetIt.
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
Did you forget to register it?)
如果我也这样注册课程。
@lazySingleton
StreamSubcription get streamSubcription => StreamSubcription();
然后它抛出编译时错误抽象类不能像我注册的所有 3rd 方类一样实例化。如何注入抽象类?有没有其他方法可以做到这一点?还是我不应该将 StreamSubcription 用于其他用途?我会很感激的
© 版权声明
本站下载的源码均来自公开网络收集转发二次开发而来,
若侵犯了您的合法权益,请来信通知我们1413333033@qq.com,
我们会及时删除,给您带来的不便,我们深表歉意。
下载用户仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,访问及下载者下载默认同意本站声明的免责申明,请合理使用切勿商用。
THE END
暂无评论内容