Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android RxJava:如何返回正确的null类型_Android_Firebase_Firebase Authentication_Kotlin_Rx Java - Fatal编程技术网

Android RxJava:如何返回正确的null类型

Android RxJava:如何返回正确的null类型,android,firebase,firebase-authentication,kotlin,rx-java,Android,Firebase,Firebase Authentication,Kotlin,Rx Java,我正在使用Firebase、Kotlin和RxJava开发一个应用程序 基本上,我需要做的是使用Firebase的Auth注册一个用户,如果用户选择了一张照片,上传照片,然后从Firebase将用户保存在数据库中 直到现在我才有这个 RxFirebaseAuth.createUserWithEmailAndPassword(auth, email, password) .map { authResult -> user.uid = a

我正在使用Firebase、Kotlin和RxJava开发一个应用程序

基本上,我需要做的是使用Firebase的Auth注册一个用户,如果用户选择了一张照片,上传照片,然后从Firebase将用户保存在数据库中

直到现在我才有这个

RxFirebaseAuth.createUserWithEmailAndPassword(auth, email, password)
            .map { authResult ->
                user.uid = authResult.user.uid
                authResult.user.uid
            }
            .flatMap<UploadTask.TaskSnapshot>(
                    { uid ->
                        if (imageUri != null)
                            RxFirebaseStorage.putFile(mFirebaseStorage
                                    .getReference(STORAGE_IMAGE_REFERENCE)
                                    .child(uid), imageUri)
                        else
                            Maybe.empty<UploadTask.TaskSnapshot>()
                    }
            )
            .map { taskSnapshot -> user.photoUrl = taskSnapshot.downloadUrl!!.toString() }
            .map {
                RxFirebaseDatabase
                        .setValue(mFirebaseDatabase.getReference("user")
                                .child(user.uid), user).subscribe()
            }
            .doOnComplete { appLocalDataStore.saveUser(user) }
            .toObservable()
RxFirebaseAuth.createUserWithEmailAndPassword(身份验证、电子邮件、密码)
.map{authResult->
user.uid=authResult.user.uid
authResult.user.uid
}
.平面图(
{uid->
if(imageUri!=null)
RxFirebaseStorage.putFile(mFirebaseStorage
.getReference(存储\图像\参考)
.child(uid),imageUri)
其他的
可能是空的
}
)
.map{taskSnapshot->user.photoUrl=taskSnapshot.downloadUrl!!.toString()}
.地图{
RxFirebaseDatabase
.setValue(mFirebaseDatabase.getReference(“用户”)
.child(user.uid),user.subscribe()
}
.doOnComplete{appLocalDataStore.saveUser(用户)}
.TooObservable()文件
当用户选择一张照片时,它工作正常,但当它未被选择时,其他贴图将被忽略,因为我返回了Maybe.empty()

我应该如何实现这一点来使用或不使用用户照片


谢谢。

请查看以下结构:

        val temp = null
        Observable.just("some value")
                .flatMap {
                    // Throw exception if temp is null
                    if (temp != null) Observable.just(it) else Observable.error(Exception("")) 
                }
                .onErrorReturnItem("") // return blank string if exception is thrown
                .flatMap { v ->
                    Single.create<String>{
                        // do something
                        it.onSuccess("Yepeee $v");
                    }.toObservable()
                }
                .subscribe({
                    System.out.println("Yes it worked: $it");
                },{
                    System.out.println("Sorry: $it");
                })
但是这段代码有一个问题,在
onerrorrurn
之前发生的任何异常都会产生一个空白uri,并导致我们不希望的进一步的链执行。如果发生任何其他异常,则应调用onError

为此,我们需要创建一个自定义异常类,并在
onerrorrurn
中捕获抛出的异常。请查看以下代码段:

...
...

.flatMap<UploadTask.TaskSnapshot>(
                        { uid ->
                            if (imageUri != null)
                                RxFirebaseStorage.putFile(mFirebaseStorage
                                        .getReference(STORAGE_IMAGE_REFERENCE)
                                        .child(uid), imageUri)
                            else
                                Observable.error<MyCustomException>(MyCustomException("null value")) // Throw exception if imageUri is null
                        }
                )
                .map { taskSnapshot -> user.photoUrl = taskSnapshot.downloadUrl!!.toString() }
                .onErrorReturn {
                    if(it !is MyCustomException)
                        throw it
                    else
                        user.photoUrl = "" // assign blank url string if exception is thrown

                }
...
...
。。。
...
.平面图(
{uid->
if(imageUri!=null)
RxFirebaseStorage.putFile(mFirebaseStorage
.getReference(存储\图像\参考)
.child(uid),imageUri)
其他的
Observable.error(MyCustomException(“null值”)//如果imageUri为null,则引发异常
}
)
.map{taskSnapshot->user.photoUrl=taskSnapshot.downloadUrl!!.toString()}
A.报税表{
如果(它是MyCustomException)
扔掉它
其他的
user.photoUrl=”“//如果引发异常,则分配空白url字符串
}
...
...
希望能有帮助

...
...

.flatMap<UploadTask.TaskSnapshot>(
                        { uid ->
                            if (imageUri != null)
                                RxFirebaseStorage.putFile(mFirebaseStorage
                                        .getReference(STORAGE_IMAGE_REFERENCE)
                                        .child(uid), imageUri)
                            else
                                Observable.error<MyCustomException>(MyCustomException("null value")) // Throw exception if imageUri is null
                        }
                )
                .map { taskSnapshot -> user.photoUrl = taskSnapshot.downloadUrl!!.toString() }
                .onErrorReturn {
                    if(it !is MyCustomException)
                        throw it
                    else
                        user.photoUrl = "" // assign blank url string if exception is thrown

                }
...
...