我已经使用干净的架构和用于状态管理的 Cubits 建立了我的项目。NetworkInfo
我还在文件夹中设置了一个类core/network
,它有一个未来的方法来检查互联网连接。我在课堂上使用它RepositoryImpl
,一切正常。但是,我目前正在努力添加互联网检查stream
以持续检查互联网状态。我不确定应该将代码添加到哪一层或如何使用streams
而不Cubit
违反干净的架构原则并确保代码的可测试性?
网络信息-
abstract class NetworkInfo {
Future<bool> isConnected();
}
class NetworkInfoImpl implements NetworkInfo {
InternetConnection internetConnection;
NetworkInfoImpl({
required this.internetConnection,
});
@override
Future<bool> isConnected() {
// TODO: implement isConnected
return internetConnection.hasInternetAccess;
}
}
存储库实现 -
class AuthRepositoryImpl implements AuthRepository {
final AuthRemoteDatasource authRemoteDatasource;
final NetworkInfo networkInfo;
AuthRepositoryImpl({
required this.authRemoteDatasource,
required this.networkInfo,
required this.crashLog,
});
@override
ResultFuture<User> emailPasswordLogin({
required String email,
required String password,
}) async {
// TODO: implement emailPasswordLogin
try {
if (!await networkInfo.isConnected()) {
return const Result.error(NoInternetFailure());
}
var user = await authRemoteDatasource.emailPasswordLogin(
email: email,
password: password,
);
return Result.success(user);
} on AuthException {
return const Result.error(AuthFailure());
} catch (exception, stack) {
return Result.error(UnknownFailure());
}
}
}