'_CastError (Null check operator used on a null value) because it requires loading
So I am creating an Instagram clone and when I open the feed screen, an error that says '_CastError (Null check operator used on a null value)' pops up. But when I left it for a minute it was replaced by the feed screen normally So it turns out it wants to load, but I am not sure how can I make it do so.
Here is the code(The bolded line is the one that requires time):
`
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],`
`),`
`);`
`}));`
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|