数据不被存储在Firebase数据库时,输入数据注册页面



当我在注册表单中添加值时,不存储数据下面是错误

错误:断言失败:文件:///G:/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core_web-1.6.2/lib/src/firebase_core_web.dart:273:11选项!= null创建默认应用程序时,FirebaseOptions不能为空。

xception被姿态 ===============================================================下面的JSNoSuchMethodError在处理手势时被抛出:TypeError: Cannot read property of null (reading 'name')

代码

import 'dart:html';
import 'package:fchat/CompleteProfile.dart';
import 'package:fchat/UserModel.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class SignUpPage extends StatefulWidget {
SignUpPage({Key? key}) : super(key: key);
@override
State<SignUpPage> createState() => _SignUpPageState();
}
var signup;
class _SignUpPageState extends State<SignUpPage> {
TextEditingController emailController=TextEditingController();
TextEditingController passwordController=TextEditingController();
TextEditingController cpasswordController=TextEditingController();
void checkValues() {
String email = emailController.text.trim();
String password = passwordController.text.trim();
String cpassword = cpasswordController.text.trim();
if (email == "" || password == "" || cpassword == "") {
print("plz filled all the field");
}
else if (password != cpassword) {
print("password did not Match");
}

else {
signup(email, password);
}

void Signup(String email, String password) async {
UserCredential? credential;

try {
credential = await FirebaseAuth.instance.createUserWithEmailAndPassword
(email: email, password: password);
}
on FirebaseAuthException catch (ex) {
print(ex.code.toString());
}
if (credential != null) {
String uid = credential.user!.uid;
UserModel newUser = UserModel(
uid: uid,
email: email,
fullname: "",
profilepic: "",
);
await FirebaseFirestore.instance.collection("users").doc(uid).set
(newUser.toMap()).then((value){
print("new user will created");
});
}
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
padding: EdgeInsets.symmetric(
horizontal: 40,
),
child: Center(
child: SingleChildScrollView(
child: Column(
children: [

Text("Chat App", style: TextStyle(
color: Theme
.of(context)
.colorScheme
.secondary,
fontSize: 40,
fontWeight: FontWeight.bold
),),
SizedBox(height: 10,),
TextField(
controller: emailController,
decoration: InputDecoration(
labelText: "Email Adresss"
),
),
SizedBox(height: 10,),
TextField(
controller: passwordController,
obscureText: true,
decoration: InputDecoration(
labelText: "Password"
),
),

SizedBox(height: 10,),
TextField(
controller: cpasswordController,
obscureText: true,
decoration: InputDecoration(
labelText: "Confirm Password"
),
),

SizedBox(height: 10,),
CupertinoButton(
onPressed: () {
checkValues();
/*   Navigator.push(
context,
MaterialPageRoute(
builder:(context)
{
return CompleteProfile();
}
),
);
*/

},
color: Theme
.of(context)
.colorScheme
.secondary,
child: Text("Sign Up"),
), // CupertinoButton

],
),
), // SingleChildscrollView
),
),
),

bottomNavigationBar: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Already have an account?", style: TextStyle(
fontSize: 16
),), // Textstyle // Text
CupertinoButton(
onPressed: () {
Navigator.pop(context);
},
child: Text("Log in", style: TextStyle(
fontSize: 16
),), // TextStyle // Text
), // CupertinoButton
],
), // Row
), // Container

);
}
}

您需要在main.dart中初始化Firebase。

main.dart

import 'firebase_options.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// THIS:
await Firebase.initializeApp( 
options: DefaultFirebaseOptions.currentPlatform,
);
...
}

firebase_options.dart

class DefaultFirebaseOptions {
static FirebaseOptions get currentPlatform {

// SIMPLIFIED CODE
return webProduction;
}
static const FirebaseOptions webProduction = FirebaseOptions(
apiKey: 'xxx',
appId: 'xxx',
messagingSenderId: 'xxx',
projectId: 'xxx',
authDomain: 'xxx.firebaseapp.com',
storageBucket: 'xxx.appspot.com',
measurementId: 'xxx',
);

最新更新