getter 'uid' 被调用为 null。接收者:空尝试调用:uid

时间:2021-06-23 16:45:36

标签: firebase flutter

这个错误“ “getter 'uid' 在 null.Receiver 上被调用:nullTried 调用: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();
                       },
                     ),

控制台:

======== 小部件库捕获的异常 ================================== ====================== 在构建 Br(dirty, dependencies: [_InheritedProviderScope], state: _QrState#24e19) 时抛出了以下 NoSuchMethodError: getter 'uid' 被调用为 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 (package:my_compound/Screens/Qr/qr.dart:99:45) #2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27) #3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15) #4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11) ...

1 个答案:

答案 0 :(得分:0)

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

提示:在使用 Flutter 时始终使用 dart null 安全。