我的应用程序流程如下:
包装程序->身份验证->登录
但是当我运行该应用程序时,它会显示红色屏幕和此异常:
The following NoSuchMethodError was thrown building Wrapper(dirty, dependencies: [InheritedProvider<User>], state: _WrapperState#fd39b):
The getter 'uid' was called on null.
Receiver: null
Tried calling: uid
我以前曾经运行过它,并且运行良好,但我不为什么现在它会显示此异常:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return StreamProvider<User>.value(
value: AuthService().user,
child: MaterialApp(
title: 'Sayarty',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Wrapper(),
),
);
}
}
这是包装器类:
class Wrapper extends StatefulWidget {
@override
_WrapperState createState() => _WrapperState();
}
class _WrapperState extends State<Wrapper> {
int _currentTab = 1;
final _page = [
Search(),
Home(),
Account(),
];
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
print(user.uid);
if (user == null) {
return Authenticate();
} else {
return
// Home();
}
}
答案 0 :(得分:1)
可能是因为现在您在测试print(user.uid);
之前添加了if user is == null
将其放入else:
//print(user.uid);
if (user == null) {
return Authenticate();
} else {
print(user.uid);
return
Home();
}