Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 布尔表达式不能为空?_Database_Firebase_Flutter - Fatal编程技术网

Database 布尔表达式不能为空?

Database 布尔表达式不能为空?,database,firebase,flutter,Database,Firebase,Flutter,有人知道我为什么得到失败的断言吗:布尔表达式不能为null。 如果我已经登录并退出应用程序,然后再次打开应用程序,我应该直接进入主屏幕 void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(MyApp()); } class MyApp extends StatefulWidget { // This widget is the

有人知道我为什么得到失败的断言吗:布尔表达式不能为null。 如果我已经登录并退出应用程序,然后再次打开应用程序,我应该直接进入主屏幕

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {

  // This widget is the root of your application.
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  bool userIsLoggedIn = false;

  @override
  void initState(){
    getLoggedInState();
    super.initState();
  }

  getLoggedInState()async{
    await HelperFunction.getUserLoggedInSharedPreference().then((value){
      setState(() {
        userIsLoggedIn = value;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: userIsLoggedIn ? HomeScreen() : CompanyLoadingBar(),
    );
  }
}
void main()异步{
WidgetsFlutterBinding.ensureInitialized();
等待Firebase.initializeApp();
runApp(MyApp());
}
类MyApp扩展了StatefulWidget{
//此小部件是应用程序的根。
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
bool userIsLoggedIn=false;
@凌驾
void initState(){
getLoggedInState();
super.initState();
}
getLoggedInState()异步{
等待HelperFunction.GetUserLoggedInSharedReference()。然后((值){
设置状态(){
userIsLoggedIn=值;
});
});
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
主页:userIsLoggedIn?主屏幕():companyladingbar(),
);
}
}

您的问题很可能在于将
userIsLoggedIn
设置为null的函数中。确保
getUserLoggedInSharedReference
实际返回布尔值且不为null

void getLoggedInState()异步{
最终结果=Wait HelperFunction.GetUserLoggedInSharedReference();
如果(结果!=null){
设置状态(){
userIsLoggedIn=结果;
});
}否则{
打印(“结果为空”);
}
}

如果此处的“值”可能变为空,则会出现错误。 为安全起见,将setState内的行更改为:

userIsLoggedIn = value ?? false; 

如果值为空,它会将bool设置为false。您的问题是您没有等待将来解析为实际值

这一行:

userIsLoggedIn = value;
调用生成方法时可能未到达。因为它是异步的,你不需要等待它

您可以将默认值设置为
userIsLoggedIn
,也可以设置为
false
,这将解决您眼前的问题。但这并不能解决您真正的问题,因为您的程序逻辑是异步的

您可能希望使用至少一个
FutureBuilder
来构建应用程序


请参阅:

您有一个全局进近错误。您可以在此代码中组合
传统
异步/等待
方法:

getLoggedInState()异步{
等待HelperFunction.GetUserLoggedInSharedReference()。然后((值){
设置状态(){
userIsLoggedIn=值;
});
});
}
如果使用
async/await
,则不应使用
then
方法

要实现您想要的,您应该使用以下内容:

。。。
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
家:未来建设者(
future:HelperFunction.GetUserLoggedInSharedReference(),
生成器:(上下文,快照){
if(snapshot.hasData){
//未来已准备就绪。从快照获取数据
userIsLoggedIn=snapshot.data;//bool值在这里
返回userIsLoggedIn?主屏幕():companyladingbar();
}否则{
//显示进展,而未来将完成
返回循环ProgressIndicator();
}
}
),
);
}
...
并检查从
HelperFunction.getUserLoggedInSharedReference()
返回的值。它似乎返回
null
,因为我认为您没有已保存的值。因此,您需要指定默认值:

final value=wait SharedPreferences.getInstance().readBool(名称)??虚假的;

检查调用堆栈。如果登录首选项尚未设置怎么办?@ThienLD您能在显示代码的地方发布一个答案吗?如果我这样做,错误就消失了,但当我再次登录并运行应用程序时,它会再次显示登录屏幕,因为该值为空,这意味着问题在于HelperFunction.GetUserLoggedInSharedReference()的工作方式处理它内部的东西-为什么它首先返回null?这与理解未来无关,duh。这是我的助手函数部分:
静态未来GetUserLoggeDinSharedReference()异步{SharedReferences prefs=Wait SharedReferences.getInstance();return prefs.getBool(SharedReferenceUserLoggeDinkey);}
很抱歉回答晚了,我认为最好的解决方案是查看共享_首选项页面上的使用示例,并尝试在代码中遵循它:@serby编辑了我的答案。我已更改了答案,但当我重新启动应用程序时,它会再次显示登录屏幕,而不是主屏幕。这一定是共享首选项的问题。这是我的帮助功能部分:
static Future getUserLoggedInSharedPreference()异步{SharedPreferences prefs=wait SharedPreferences.getInstance();返回prefs.getBool(sharedPreferenceUserLoggedInKey);}
跟随链接,阅读并理解它,然后为您的代码实现它。如果你在做这件事时有什么特别的问题,尽管问吧。