当前位置: 首页 > 知识库问答 >
问题:

在null上调用getter“uid”。接收者:null尝试调用:uid

袁波
2023-03-14

此错误“”getter'uid'在null上被调用。receiver:null尝试调用:uid“


class Wrapper extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final user = Provider.of<User_fb>(context);

    // return home or login
    if (user == null) {
      return Login();
    } else {
      print(user.uid);

      return Home();
    }
  }
}

Future Signinwithemailandpassword(var Res_email,var Res_Password) async{

    try{
        UserCredential result= await _auth.signInWithEmailAndPassword(email: Res_email, password: Res_Password);
        User user = result.user;
        return _userfromfirebase(user);
    }catch(e){
      return null;
    }

}

Future signOut () async{
try{
  return await _auth.signOut();
}
catch(e){
print(e.toString());
print("error at signOut ");
return null;
}
}
void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp();

  runApp(Main());
}

class Main extends StatefulWidget {
  @override
  _MainState createState() => _MainState();
}

class _MainState extends State<Main> {
  @override
  Widget build(BuildContext context) {

    return StreamProvider<User_fb>.value(
      value: Authservice().user,
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
            primaryColor: Color.fromRGBO(255, 255, 255, 1),
            shadowColor: Colors.transparent,
            highlightColor: Colors.transparent),
        home: Wrapper(),

      ),
    );
  }
}

退出按钮:

ListTile(
                       leading: Icon(Ionicons.log_out_outline,
                           color: Color.fromRGBO(86, 197, 150, 1)),
                       title: Text('Log out'),
                       onTap: () async {
                         await _auth.signOut();
                       },
                     ),

html" target="_blank">控制台:

widgets库捕获的异常接收者:null尝试调用:uid

相关的导致错误的小部件是:Qr file://g://androidstudioprojects/facem/lib/screens/home/home.dart:687:67当抛出异常时,这是堆栈:#0 object.nosuchmethod(dart:core-patch/object_patch.dart:54:5)#1_qrstate.build(包:my_compound/screens/Qr/Qr.dart:99:45)#2 statefulelement.build(包Fulelement.performRebuild(包:flutter/src/widgets/framework.dart:4746:11)...

共有1个答案

邢修明
2023-03-14

发生这种情况是因为user为null,所以使用null检查运算符。像这样的用户?。uid或user.uid??'',

提示:在使用颤振时,始终使用省道零安全。

 类似资料: