打开屏幕时出错:(child:) 必须是非空字符串且不包含
Posted
技术标签:
【中文标题】打开屏幕时出错:(child:) 必须是非空字符串且不包含【英文标题】:Error when open screen: (child:) Must be a non-empty string and not contain 【发布时间】:2021-08-11 21:49:51 【问题描述】:我正在尝试在我的应用上打开注册屏幕,但应用崩溃了,我在下方收到此错误。我什至无法打开屏幕,所以我认为电子邮件地址上的任何点都没有问题。有人可以帮我解决这个问题吗?
注意:我是 ios 开发的初学者,很抱歉我的英语不好。 :-)
谢谢。
错误:
(child:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']''
我的代码:
struct SignUpView: View
@State var username: String = ""
@State var email: String = ""
@State var password: String = ""
@State var confrim_password = ""
@State private var showingAlert = false
@State var dialogErrorMsg: String? = ""
@State var singedIn = false
var ref: DatabaseReference! = Database.database().reference()
@EnvironmentObject var viewModel: AppViewModel
func signIn(email: String, password: String)
Auth.auth().signIn(withEmail: email, password: password) result, error in
if result != nil, error == nil
singedIn = true
else
dialogErrorMsg = error?.localizedDescription
showingAlert = true
func signUp(username: String, email: String, password: String)
Auth.auth().createUser(withEmail: email, password: password) result, error in
if result != nil, error == nil
let user = ["email": email,
"name": username,
"id": Auth.auth().currentUser?.uid,
"photoUrl": "none",
"premium": "false"]
// Update display name on FirebaseAuth
let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
changeRequest?.displayName = username
changeRequest?.commitChanges()
// Add user to Real-Time DB
self.ref.child(Auth.auth().currentUser?.uid ?? "none").setValue(user)
else
showingAlert = true
dialogErrorMsg = error?.localizedDescription
var body: some View
NavigationView
ScrollView
VStack
Image("logo_transparent").resizable().scaledToFit()
.frame(width: 200, height: 200, alignment: .top)
.padding(.top, -40)
Text("Sign up")
.bold()
.font(.system(size: 40))
TextField("Usename", text: $username)
.frame(height: 45)
.textFieldStyle(PlainTextFieldStyle())
.padding([.horizontal], 4)
.cornerRadius(16)
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color.gray))
.padding([.horizontal], 24)
TextField("Email", text: $email)
.frame(height: 45)
.textFieldStyle(PlainTextFieldStyle())
.padding([.horizontal], 4)
.cornerRadius(16)
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color.gray))
.padding([.horizontal], 24)
.padding(.top, 13)
.autocapitalization(.none)
SecureField("Password", text: $password)
.frame(height: 45)
.textFieldStyle(PlainTextFieldStyle())
.padding([.horizontal], 4)
.cornerRadius(16)
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color.gray))
.padding([.horizontal], 24)
.padding(.top, 13)
SecureField("Confirm Password", text: $confrim_password)
.frame(height: 45)
.textFieldStyle(PlainTextFieldStyle())
.padding([.horizontal], 4)
.cornerRadius(16)
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color.gray))
.padding([.horizontal], 24)
.padding(.top, 13)
.padding(.bottom, 25)
Text("Sign up")
.bold()
.font(.system(size: 25))
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 50)
.background(Color.blue)
.cornerRadius(40)
.onTapGesture
if username == "" || email == "" || password == "" || confrim_password == ""
showingAlert = true
dialogErrorMsg = "Please fill all the values"
else
if password != confrim_password
showingAlert = true
dialogErrorMsg = "Passwords do not match"
else
signUp(username: username, email: email, password: password)
signIn(email: email, password: password)
.alert(isPresented: $showingAlert, content:
Alert(title: Text("Error"), message: Text(dialogErrorMsg!), dismissButton: .default(Text("Got it!")))
)
// Navigate to SelectProfileImageView when the user is singed in
NavigationLink("", destination: SelectProfileImageView().navigationTitle("Set Profile Picture").navigationBarTitleDisplayMode(.inline), isActive: $singedIn)
NavigationLink(destination:
LoginView().navigationBarHidden(true))
Text("Already have an account? Sing in")
.bold()
.font(.system(size: 20))
.padding(.bottom, 300)
.padding(.top, 20)
.navigationBarHidden(true)
.navigationBarHidden(true)
.edgesIgnoringSafeArea(.all)
.navigationBarHidden(true)
.navigationTitle("")
struct SignUpView_Previews: PreviewProvider
static var previews: some View
SignUpView()
【问题讨论】:
是来自 github 的“SVProgressHUD”库。如果是这样,我认为它不适合在 SwiftUI 中使用,尤其是试图通过你的函数 signUp 和 signIn 从 onTapGesture 中弹出一些东西。重新考虑没有这个库的 SwiftUI 代码。 @workingdog 我尝试在不使用此库的情况下运行该应用程序,但没有任何改变 你的代码(没有 SVProgressHUD)对我来说效果很好,即使是 3 ".navigationBarHidden(true)" 向我们展示您在没有库的情况下使用的代码。 好消息。您能否展示您如何调用您的 SignUpView。想想看,错误可能来自代码中的任何地方。显然不是来自 SignUpView。 【参考方案1】:我在这里猜测错误发生在:
// Add user to Real-Time DB
self.ref.child(Auth.auth().currentUser?.uid ?? "none").setValue(user)
您可以尝试用类似的东西替换它,根据您的需要进行调整:
if let userid = Auth.auth().currentUser?.uid
self.ref.child("users/\(userid)/").setValue(user)
【讨论】:
我试过了,但是当我尝试打开这个屏幕时仍然出现这个错误以上是关于打开屏幕时出错:(child:) 必须是非空字符串且不包含的主要内容,如果未能解决你的问题,请参考以下文章
键必须是非空字符串,并且不能包含“.”、“#”、“$”、“/”、“[”或“]”
如何解决node.js中的“消息内容必须是非空字符串”[关闭]