在此<User>主屏幕页面小部件上方找不到正确的提供程序



我收到错误:在此MainScreenPage小部件上找不到正确的提供程序。我无法弄清楚我的代码出了什么问题,因为我的MultiProvider是MaterialApp的父级。

我尝试过但没有帮助的事情:

  1. 已停止调试并开始调试
  2. 将MultiProvider移动到runApp中,使MyApp成为其子程序

如果有人能帮忙,我将不胜感激。

调试控制台上显示的内容导致错误的相关小部件MainScreenPage lib/main.dart:22(即"widget=MainScreenPage(("(

<main.dart>

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
User? firebaseUser = FirebaseAuth.instance.currentUser;
Widget widget;
if (firebaseUser != null) {
print(firebaseUser.email);
widget = MainScreenPage();
} else {
widget = LoginPage();
}
return MultiProvider(
providers: [
StreamProvider<User?>(
initialData: null,
create: (context) => FirebaseAuth.instance.authStateChanges(),
)
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'BookTracker',
theme: ThemeData(
primarySwatch: Colors.blue,
textTheme: GoogleFonts.notoSansTextTheme(
Theme.of(context).textTheme,
),
),
home: widget,
),
);
}
}

<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: [
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: [
InkWell(
child: SizedBox(
height: 40,
width: 40,
child: CircleAvatar(
radius: 60,
backgroundImage: NetworkImage(curUser.avatarUrl ??
'https://picsum.photos/id/1022/300'),
backgroundColor: Colors.white,
child: Text(''),
),
),
onTap: () {
showDialog(
context: context,
builder: (context) {
return createProfileDialog(
context, curUser, userBooksReadList);
},
);
},
),
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) != null ? book.rating : 0,
),
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) != null ? book.rating : 0,
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));
},
),
],
),
);
}
}

"var authUser = Provider.of<User>(context);"你能给这个语法加个问号吗?它应该是这样的:"var authUser = Provider.of<User?>(context);"

最新更新