【发布时间】:2022-06-27 00:35:09
【问题描述】:
所以我正在创建一个 Instagram 克隆,当我打开提要屏幕时,会弹出一个错误,显示“_CastError(用于空值的空检查运算符)”。 但是当我离开它一分钟时,它通常被饲料屏幕取代 所以事实证明它想要加载,但我不知道如何让它这样做。
代码如下(粗体部分需要时间):
`
class PostCard extends StatefulWidget {`
`final snap;`
`const PostCard({Key? key, required this.snap}) : super(key: key);`
`@override`
`State<PostCard> createState() => _PostCardState();`
`}`
`class _PostCardState extends State<PostCard> {`
`bool isLikeAnimating = false;`
`int commentLen = 0;`
`bool isLoading = false;`
`@override`
`Widget build(BuildContext context) {`
`
** final UserProvider userProvider = Provider.of<UserProvider>(context);**`
`return isLoading`
`? const Center(`
`child: CircularProgressIndicator(),`
`)`
`: Container(`
`color: Colors.white,`
`padding: EdgeInsets.symmetric(vertical: 10),`
`child: Column(`
`children: [`
`Container( //inside here are the contents of the post`
`);`
`}`
`}`
`Feed Screen:`
`StreamBuilder(`
`stream: stream,`
`builder: (context,`
`AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {`
`if (snapshot.connectionState == ConnectionState.waiting) {`
`return Center(`
`child: CircularProgressIndicator(),`
`);`
`}`
`return ListView.builder(`
`itemCount: snapshot.data!.docs.length,`
`itemBuilder: (context, index) => PostCard(`
`snap: snapshot.data!.docs[index],`
`),`
`);`
`}));`
【问题讨论】: