我无法在Firebase中进行新收藏

我无法在firebase上创建新的集合用户。我正在ScopedModel中进行状态管理,并且我知道数据库与应用程序连接良好,所以我不知道到底出了什么问题,我只是知道每次注册都会失败。如果有必要,我可以共享github存储库,以便大家仔细查看该项目。我真的很需要帮助,因此对有意见的人都开放。

userModel

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:scoped_model/scoped_model.dart';

class UserModel extends Model {
  FirebaseAuth _auth = FirebaseAuth.instance;
  FirebaseUser firebaseUser;
  Map<String, dynamic> userData = Map();
  bool isLoading = false;

  void signIn() async {
    isLoading = true;
    notifyListeners();
    await Future.delayed(Duration(seconds: 3));
    isLoading = false;
    notifyListeners();
  }

  void signUp (
      {@required Map<String, dynamic> userData,
      @required String pass,
      @required VoidCallback onSuccess,
      @required VoidCallback onFail}) {
    isLoading = true;
    notifyListeners();
    _auth
        .createUserWithEmailAndPassword(
            email: userData["email"], password: pass)
        .then((user) async {
      firebaseUser = user.user;
      print(firebaseUser.uid);
      await _saveUserData(userData);
      onSuccess();
      isLoading = false;
      notifyListeners();
    }).catchError((e) {
      onFail();
      isLoading = false;
      notifyListeners();
    });
  }

  void recoverPass() {}

  Future<Null> _saveUserData(Map<String, dynamic> userData) async {
    this.userData = userData;
    await Firestore.instance
        .collection("users")
        .document(firebaseUser.uid)
        .setData(userData);
  }
}

主要

import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'Src/models/user.dart';
import 'Src/screens/sign_up_screen.dart';


void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ScopedModel<UserModel>(
      model: UserModel(),
      child:  MaterialApp(
        title: 'Welcome to Flutter',
        debugShowCheckedModeBanner: false,
        home:SignUpScreen(),
      ),
    );
  }
}

注册屏幕

import 'package:flutter/material.dart';
import 'package:projectfooddelivery/Src/models/user.dart';
import 'package:scoped_model/scoped_model.dart';


class SignUpScreen extends StatefulWidget {



  @override
  _SignUpScreenState createState() => _SignUpScreenState();
}

class _SignUpScreenState extends State<SignUpScreen> {
  final _nameController=TextEditingController();
  final _surnameController=TextEditingController();
  final _emailController=TextEditingController();
  final _passController=TextEditingController();
  final _scaffoldKey = GlobalKey<ScaffoldState>();
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {

    return Scaffold(
        key: _scaffoldKey,
        body:ScopedModelDescendant<UserModel>(

          builder: (context,child,model){
            if(model.isLoading)
              return Center(child:CircularProgressIndicator(),);
            return Form(
              key:_formKey,
              child: ListView(
                children: <Widget>[
                  Column(
                    children: <Widget>[
                      Center(
                        child: Icon(Icons.person,size: 100,color: Theme.of(context).primaryColor,),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(12),
                        child: Container(
                          decoration: BoxDecoration(
                              border: Border.all(color: Colors.grey),
                              borderRadius: BorderRadius.circular(15)
                          ),
                          child: Padding(padding: EdgeInsets.only(left: 10),
                            child: TextFormField(
                              controller: _nameController,
                              decoration: InputDecoration(
                                  border: InputBorder.none,
                                  hintText: "Name",
                                  icon: Icon(Icons.person)
                              ),
                            ),
                          ),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(12),
                        child: Container(
                          decoration: BoxDecoration(
                              border: Border.all(color: Colors.grey),
                              borderRadius: BorderRadius.circular(15)
                          ),
                          child: Padding(padding: EdgeInsets.only(left: 10),
                            child: TextFormField(
                              controller: _surnameController,
                              decoration: InputDecoration(
                                  border: InputBorder.none,
                                  hintText: "Surname",
                                  icon: Icon(Icons.person)
                              ),
                            ),
                          ),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(12),
                        child: Container(
                          decoration: BoxDecoration(
                              border: Border.all(color: Colors.grey),
                              borderRadius: BorderRadius.circular(15)
                          ),
                          child: Padding(padding: EdgeInsets.only(left: 10),
                            child: TextFormField(
                              controller: _emailController,
                              decoration: InputDecoration(
                                  border: InputBorder.none,
                                  hintText: "E-mail",
                                  icon: Icon(Icons.email)
                              ),
                            ),
                          ),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(12),
                        child: Container(
                          decoration: BoxDecoration(
                              border: Border.all(color: Colors.grey),
                              borderRadius: BorderRadius.circular(15)
                          ),
                          child: Padding(padding: EdgeInsets.only(left: 10),
                            child: TextFormField(
                              controller:_passController,
                              decoration: InputDecoration(
                                  border: InputBorder.none,
                                  hintText: "Password",
                                  icon: Icon(Icons.lock)
                              ),
                            ),
                          ),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(10),
                        child: GestureDetector(
                          onTap: (){
                            if(_formKey.currentState.validate()){
                              Map<String,dynamic> userData={
                                "name":_nameController.text,
                                "surname":_surnameController.text,
                                "email":_emailController.text,

                              };
                              model.signUp(
                                  userData: userData,
                                  pass: _passController.text,
                                  onSuccess: _onSuccess,
                                  onFail: _onFail
                              );
                            }
                          },
                          child: Container(
                            width: 200,
                            decoration: BoxDecoration(
                                color: Theme.of(context).primaryColor,
                                border: Border.all(color: Colors.grey),
                                borderRadius: BorderRadius.circular(25)
                            ),
                            child: Padding(padding: EdgeInsets.only(top: 10, bottom: 10,),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  Text("Register ",
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold,
                                        fontSize: 22,
                                        color: Colors.white
                                    ),
                                  )
                                ],
                              ),
                            ),
                          ),
                        ),
                      ),
                      GestureDetector(child: Text("Already have a account? Sign up here")),
                    ],
                  ),
                ],
              ),
            );

          },
        )
    );

  }
  void _onSuccess(){
    print('Regstration Successful');
  }
  void _onFail(){
    print('Regstration failed');
  }

}
评论