getter'username'在null上被调用

时间:2019-09-11 20:56:41

标签: firebase flutter dart

获取器“用户名”在null上被调用。

 class Register extends StatefulWidget {

 final User user;
 Register({this.user});

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


class RegisterState extends State<Register> {
final User user;
RegisterState({this.user});

FirestoreService fireServ = new FirestoreService();

TextEditingController _userNameController;
TextEditingController _userEmailController;
TextEditingController _userPasswordController;
TextEditingController _userMobileController;
TextEditingController _userLocationController;
TextEditingController _userBdController;



@override
 void initState() {
super.initState();
_userNameController = new TextEditingController(text:     '     ${this.user.username}');
_userEmailController = new TextEditingController(text: widget.user.useremail);
_userPasswordController = new TextEditingController(text: widget.user.userpassword);
_userMobileController = new TextEditingController(text: widget.user.usermobile);
_userLocationController = new TextEditingController(text: widget.user.userlcation);
_userBdController = new TextEditingController(text: widget.user.userbd);

}

final FirebaseAuth auth = FirebaseAuth.instance;

final formKey = new GlobalKey<FormState>();

 createData() {
 fireServ.createUserList(_userNameController.text,    _userEmailController.text, _userPasswordController.text, _userMobileController.text, _userLocationController.text, _userBdController.text).then((_) {
  Navigator.pop(context);
});

}

void signUp() async {
if (formKey.currentState.validate()) {
  formKey.currentState.save();
  try {
    FirebaseAuth.instance
        .createUserWithEmailAndPassword(
            email: _userEmailController.text,
            password: _userPasswordController.text)
        .then((signedUser) {
      createData();
    });
  } catch (e) {
    print(e.message);
  }
}

}

  @override
  Widget build(BuildContext context) {
  // TODO: implement build
  return Scaffold(
  appBar: AppBar(
    title: Text('RegisterPage'),
  ),
  body: ListView(children: <Widget>[
    Container(
      child: Form(
        key: formKey,
        child: Column(
          children: loginCard() + SocialMedia(),
        ),
      ),
    )
  ],)
);

}

List<Widget> loginCard() {
  return [
   Padding(
    padding: const EdgeInsets.only(top: 50),
    child: Stack(
      children: <Widget>[
        Container(
          padding: EdgeInsets.only(top: 20),
          child: Column(
            children: <Widget>[
              Center(
                child: Container(
                  width: 330.0,
                  height: 500.0,
                  color: Colors.white.withOpacity(.1),
                  child: Container(
                    decoration: new BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.all(Radius.circular(10)),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black54,
                          offset: Offset(2, 3),
                          blurRadius: 10,
                        )
                      ],
                    ),
                    child: Container(
                      padding:
                          EdgeInsets.only(left: 20, top: 40, right: 15),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(
                            'Register',
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 30),
                          ),
                          TextFormField(
                            controller: _userNameController,
                            decoration: InputDecoration(
                              labelText: 'name',
                              labelStyle: TextStyle(
                                  color: Colors.grey,
                                  fontStyle: FontStyle.italic),
                              icon: Icon(
                                Icons.person,
                                size: 25,
                              ),
                              hintText: 'your name here',
                              hintStyle:
                                  Theme.of(context).textTheme.caption,
                            ),
                            textCapitalization: TextCapitalization.words,
                            validator: (value)=>value.isEmpty?'Name can\'t be embty':null,
                            onSaved: (value) => _userNameController.text = value,
                          ),
                          TextFormField(
                            controller: _userEmailController,
                            decoration: InputDecoration(
                              labelText: 'email',
                              labelStyle: TextStyle(
                                  color: Colors.grey,
                                  fontStyle: FontStyle.italic),
                              icon: Icon(
                                Icons.email,
                                size: 25,
                              ),
                              hintText: 'your email here',
                              hintStyle: TextStyle(
                                  color: Colors.blueGrey, fontSize: 10),
                            ),
                            keyboardType: TextInputType.emailAddress,
                            enableInteractiveSelection: true,
                            validator: (value)=>value.isEmpty?'Email can\'t be embty':null,
                            onSaved: (value) => _userEmailController.text = value,
                          ),
                          TextFormField(
                            controller: _userPasswordController,
                            obscureText: true,
                            decoration: InputDecoration(
                              labelText: 'password',
                              labelStyle: TextStyle(
                                  color: Colors.grey,
                                  fontStyle: FontStyle.italic),
                              icon: Icon(
                                Icons.lock,
                                size: 23,
                              ),
                              hintText: 'your password here',
                              hintStyle: TextStyle(
                                  color: Colors.blueGrey, fontSize: 10),
                            ),
                            validator: (value)=>value.isEmpty?'Password can\'t be embty':null,
                            onSaved: (value) => _userPasswordController.text = value,
                          ),
                          TextFormField(
                            controller: _userMobileController,
                            decoration: InputDecoration(
                              labelText: 'Mobile Number',
                              labelStyle: TextStyle(
                                  color: Colors.grey,
                                  fontStyle: FontStyle.italic),
                              icon: Icon(
                                Icons.lock,
                                size: 23,
                              ),
                              hintText: 'your number here',
                              hintStyle: TextStyle(
                                  color: Colors.blueGrey, fontSize: 10),
                            ),
                            validator: (value)=>value.isEmpty?'Mobile can\'t be embty':null,
                            onSaved: (value) => _userMobileController.text = value,
                          ),
                          TextFormField(
                            controller: _userLocationController,
                            decoration: InputDecoration(
                              labelText: 'Location',
                              labelStyle: TextStyle(
                                  color: Colors.grey,
                                  fontStyle: FontStyle.italic),
                              icon: Icon(
                                Icons.lock,
                                size: 23,
                              ),
                              hintText: 'your Location here',
                              hintStyle: TextStyle(
                                  color: Colors.blueGrey, fontSize: 10),
                            ),
                            validator: (value)=>value.isEmpty?'Location can\'t be embty':null,
                            onSaved: (value) => _userLocationController.text = value,
                          ),
                          TextFormField(
                            controller: _userBdController,
                            decoration: InputDecoration(
                              labelText: 'Birthday',
                              labelStyle: TextStyle(
                                  color: Colors.grey,
                                  fontStyle: FontStyle.italic),
                              icon: Icon(
                                Icons.lock,
                                size: 23,
                              ),
                              hintText: 'your number here',
                              hintStyle: TextStyle(
                                  color: Colors.blueGrey, fontSize: 10),
                            ),
                            validator: (value)=>value.isEmpty?'Mobile can\'t be embty':null,
                            onSaved: (value) => _userBdController.text = value,
                          ),
                          Container(
                            padding:
                                const EdgeInsets.only(left: 205, top: 10),
                            decoration: BoxDecoration(
                                borderRadius:
                                    BorderRadius.all(Radius.circular(20))),
                            child: Row(
                              children: <Widget>[
                                RaisedButton(
                                  child: Text(
                                    'Sign Up',
                                    style: TextStyle(color: Colors.white),
                                  ),
                                  color: Colors.orange,
                                  onPressed: () => signUp(),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ],
    ),
  ),
];

}

主页

 void main() => runApp(MaterialApp(

 home: MyApp(),

 ));


 class MyApp extends StatelessWidget {



  @override

 Widget build(BuildContext context) {

return MaterialApp(
  theme: basicTheme(),
  debugShowCheckedModeBanner: false,
  title: 'Navigation',
  routes: <String, WidgetBuilder>{
    '/Login': (BuildContext context) => Login(),
    '/Register': (BuildContext context) => Register(),
    '/Profile': (BuildContext context) => Profile(),
    '/carsPage': (BuildContext context) => carsPage(),
    '/MyAppBar': (BuildContext context) => MyAppBar(),
    '/EditProfile': (BuildContext dynamic) => EditProfile01(),
    '/GarageitemPage': (BuildContext context) => GarageitemPage(),
    '/CarItemPage': (BuildContext context) => CarItemPage(),
    '/washPagePage': (BuildContext context) => washPage(),
    '/reviewsPage': (BuildContext context) => reviewsPage(),
    '/WashItemPage': (BuildContext context) => WashItemPage(),
    '/AddGarageAds': (BuildContext context) => AddGarageAds(),
    '/garagePage': (BuildContext context) => garagePage(),
    '/AddGarageAdsFirebase': (BuildContext context) => AddGarageAdsFirebase(),
   },
   home: Login(),
  );
  }
 }

════════小部件库捕获到异常══════════════════════════════════ ═════════════════════ 在构建Builder时引发了以下NoSuchMethodError: 获取器“用户名”在null上被调用。 接收者:null 尝试致电:用户名

由用户创建的导致错误的小部件的祖先是:   MaterialApp文件:///Users/ebroo/IdeaProjects/car_app_firebase_03/lib/main.dart:44:12 引发异常时,这是堆栈:

0 Object.noSuchMethod(dart:core-patch / object_patch.dart:51:5)

1 RegisterState.initState(程序包:car_app_firebase_03 / ui_test / register_page.dart:37:72)

2 StatefulElement._firstBuild(package:flutter / src / widgets / framework.dart:4061:58)

3 ComponentElement.mount(package:flutter / src / widgets / framework.dart:3912:5)

4 Element.inflateWidget(package:flutter / src / widgets / framework.dart:3094:14)

...

2 个答案:

答案 0 :(得分:0)

我看到您在对象上调用username的唯一地方是在_userNameController = new TextEditingController(text: ' ${this.user.username}')中,因此看起来this.user为空。

如果这确实是原因,您可能要尝试${this.user?.username},以确保如果.username为空,this.user不会得到评估。

答案 1 :(得分:0)

从我提供的代码的角度来看,您似乎没有将任何内容传递给导致空指针的 Register 类。

您可以做类似的事情 注册(用户();

另一件事,您可以为构造函数添加@required,以帮助您的编译器检查错误 注册({@required this.user});