1

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

*Null check operator used on a null value  
When the exception was thrown, this was the stack:   
#0      MainScreenPage.build.<anonymous closure>.<anonymous closure>
 (package:flutter_book_tracker_app/screens/main_screen_page.dart:274:49)

Null check operator used on a null value  
When the exception was thrown, this was the stack:   
#0      MainScreenPage.build.<anonymous closure>.<anonymous closure> (package:flutter_book_tracker_app/screens/main_screen_page.dart:66:45)

A RenderFlex overflowed by 199462 pixels on the bottom.  
The relevant error-causing widget was: 
  Column file:///Users/junko/AndroidStudioProjects/flutter_book_tracker_app/flutter_book_tracker_app/lib/screens/main_screen_page.dart:132:13

A RenderFlex overflowed by 99671 pixels on the right.
The relevant error-causing widget was: 
  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 并显示“在空值上使用空检查运算符”和“A RenderFlex 在底部溢出 199462 像素”错误。

<main_screen_page.dart>

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(),
            builder: (context, 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,
          ),
          StreamBuilder<QuerySnapshot>(
            stream: bookCollectionReference.snapshots(),
            builder: (context, 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 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,
                          itemBuilder: (context, index) {
                            Book book = readingListListBook[index];
                            return InkWell(
                              child: ReadingListCard(
                                buttonText: '未読',
                                rating: book.rating!,
                                image: book.photoUrl!,
                                title: book.title,
                                author: book.author!,
                                isBookRead: false,
                              ),
                              onTap: () {
                                showDialog(
                                  context: context,
                                  builder: (context) {
                                    return BookDetailDialog(
                                      book: book,
                                    );
                                  },
                                );
                              },
                            );
                          },
                        )
                      : Text('表示する書籍データがありません ^_^;',
                          style: Theme.of(context).textTheme.headline6));
            },
          ),
        ],
      ),
    );
  }
}

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

我尝试通过在每次出现 authUser! 之前添加“if (authUser == null) {}”来解决空值检查问题,结果出现错误“setState() 或 markNeedsBuild() 在构建期间调用”和“'package:flutter /src/widgets/navigator.dart':断言失败:第 4564 行 pos 12:'!_debugLocked':不正确。” (在相同的两个 RenderFlex 问题之上。)

谁能帮我解决这些问题?

4

0 回答 0