微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

退出后显示“用于空值的空检查运算符”

如何解决退出后显示“用于空值的空检查运算符”

在我的应用上从 firebase 注销后,我收到以下消息。

*空检查运算符用于空值
抛出异常时,这是堆栈:
#0 MainScreenPage.build.. (包:Flutter_book_tracker_app/screens/main_screen_page.dart:274:49)

用于空值的空检查运算符
抛出异常时,这是堆栈:
#0 MainScreenPage.build.. (package:Flutter_book_tracker_app/screens/main_screen_page.dart:66:45)

RenderFlex 在底部溢出了 199462 像素。
相关的导致错误的小部件是: 列文件:///Users/junko/AndroidStudioProjects/Flutter_book_tracker_app/Flutter_book_tracker_app/lib/screens/main_screen_page.dart:132:13

右侧的 RenderFlex 溢出了 99671 像素。 相关的导致错误的小部件是: AppBar file:///Users/junko/AndroidStudioProjects/Flutter_book_tracker_app/Flutter_book_tracker_app/lib/screens/main_screen_page.dart:36:15*

  1. 显示的所有错误都在 main_screen_page.dart 上,但退出后模拟器窗口显示 login_screen_page。
  2. 单击模拟器上的 [后退] 按钮时,模拟器显示 main_screen_page.dart 和“用于空值的空检查运算符”和“底部的 RenderFlex 溢出 199462 像素”错误

class MainScreenPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    CollectionReference userCollectionReference =
        FirebaseFirestore.instance.collection('users');
    CollectionReference bookCollectionReference =
        FirebaseFirestore.instance.collection('books');
    List<Book> userBooksReadList = [];
    // int booksRead = 0;

    var authUser = Provider.of<User?>(context);

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white24,elevation: 0,toolbarHeight: 77,centerTitle: false,title: Row(
          children: [
            Image.asset(
              'assets/images/Icon-76.png',scale: 2,),Text(
              'A.Reader',style: Theme.of(context).textTheme.headline6!.copyWith(
                  color: Colors.redAccent,fontWeight: FontWeight.bold),],actions: [
          StreamBuilder<QuerySnapshot>(
            stream: userCollectionReference.snapshots(),builder: (context,snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting) {
                return Center(
                  child: CircularProgressIndicator(),);
              }
              final userListStream = snapshot.data!.docs.map((user) {
                return MUser.fromDocument(user);
              }).where((user) {
                return (user.uid == authUser!.uid);
              }).toList();

              MUser curUser = userListStream[0];

              return Column(
                children: [
                  SizedBox(
                    height: 40,width: 40,child: InkWell(
                      onTap: () {
                        showDialog(
                          context: context,builder: (context) {
                            return createProfileDialog(
                                context,curUser,userBooksReadList);
                          },);
                      },child: CircleAvatar(
                        radius: 60,backgroundImage: NetworkImage(curUser.avatarUrl ??
                            'https://picsum.photos/id/1022/300'),backgroundColor: Colors.white,child: Text(''),Text(
                    curUser.displayName.toupperCase(),overflow: TextOverflow.ellipsis,style: TextStyle(color: Colors.black),)
                ],);
            },TextButton.icon(
            onpressed: () {
              FirebaseAuth.instance.signOut().then((value) {
                return Navigator.push(
                    context,MaterialPageRoute(
                      builder: (context) => LoginPage(),));
              });
            },icon: Icon(Icons.logout),label: Text(''),floatingActionButton: FloatingActionButton(
        onpressed: () {
          Navigator.push(
              context,MaterialPageRoute(
                builder: (context) => BookSearchPage(),));
        },child: Icon(
          Icons.add,backgroundColor: Colors.redAccent,body: Column(
        children: [
          Container(
            width: double.infinity,margin: const EdgeInsets.only(top: 12,left: 12,bottom: 10),child: RichText(
              text: TextSpan(
                  style: Theme.of(context).textTheme.headline6,children: [
                    TextSpan(text: '今、'),TextSpan(
                        text: '読んでいるのは',style: TextStyle(fontWeight: FontWeight.w600)),TextSpan(text: '…'),]),SizedBox(
            height: 10,StreamBuilder<QuerySnapshot>(
            stream: bookCollectionReference.snapshots(),snapshot) {
              if (snapshot.hasError) {
                return Text('問題が発生しました (T.T)');
              }
              if (snapshot.connectionState == ConnectionState.waiting) {
                return Expanded(
                    flex: 1,child: Center(
                        child: CircularProgressIndicator(
                      strokeWidth: 2,color: Colors.lime.shade300,)));
              }

              if (snapshot.data!.docs.isEmpty) {
                return Text('表示する書籍データがありません',style: Theme.of(context).textTheme.headline4);
              }

              final userBookFilteredReadListStream =
                  snapshot.data!.docs.map((book) {
                return Book.fromDocument(book);
              }).where((book) {
                return ((book.userId == authUser!.uid)) &&
                    (book.finishedReading == null) &&
                    (book.startedReading != null);
              }).toList();

              userBooksReadList = snapshot.data!.docs.map((book) {
                return Book.fromDocument(book);
              }).where((book) {
                return ((book.userId == authUser!.uid)) &&
                    (book.finishedReading != null) &&
                    (book.startedReading != null);
              }).toList();

              return Expanded(
                  flex: 1,child: (userBookFilteredReadListStream.length > 0)
                      ? ListView.builder(
                          scrollDirection: Axis.horizontal,itemCount: userBookFilteredReadListStream.length,itemBuilder: (context,index) {
                            Book book = userBookFilteredReadListStream[index];

                            return InkWell(
                              child: ReadingListCard(
                                buttonText: '読書中',image: book.photoUrl!,title: book.title,author: book.author!,rating: book.rating!,onTap: () {
                                showDialog(
                                  context: context,builder: (context) {
                                    return BookDetailDialog(
                                      book: book,);
                                  },);
                              },);
                          },)
                      : Text('読書中の本はありません (・o・)',style: Theme.of(context).textTheme.headline6));
            },Container(
            width: double.infinity,margin: EdgeInsets.only(left: 10),child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,children: [
                RichText(
                  text: TextSpan(children: [
                    TextSpan(
                      text: '読みたい本リスト',style: TextStyle(
                          fontSize: 24,fontWeight: FontWeight.w600,color: kBlackColor),SizedBox(
            height: 8,)));
              }

              if (snapshot.data!.docs.isEmpty) {
                return Center(
                  child: Text('表示する書籍データがありません',style: Theme.of(context).textTheme.headline4),);
              }

              final readingListListBook = snapshot.data!.docs.map((book) {
                return Book.fromDocument(book);
                // ログイン中ユーザのuidでbookをフィルタリングする
              }).where((book) {
                return ((book.userId == authUser!.uid)) &&
                    (book.startedReading == null) &&
                    (book.finishedReading == null);
              }).toList();
              return Expanded(
                  flex: 1,child: (readingListListBook.length > 0)
                      ? ListView.builder(
                          scrollDirection: Axis.horizontal,itemCount: readingListListBook.length,index) {
                            Book book = readingListListBook[index];
                            return InkWell(
                              child: ReadingListCard(
                                buttonText: '未読',isBookRead: false,)
                      : Text('表示する書籍データがありません ^_^;',);
  }
}

“认证用户!” 退出似乎导致了空检查问题,因为退出时authUser变为空。

我尝试通过在每次出现 authUser! 之前添加“if (authUser == null) {}”来解决空检查问题,但最终出现错误“setState() 或 markNeedsBuild() 在构建期间调用”和“' package:Flutter/src/widgets/navigator.dart': 失败的断言: line 4564 pos 12: '!_debugLocked': is not true。" (在同样的两个 RenderFlex 问题之上。)

有人能帮我解决这些问题吗?非常感谢您的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。