_CastError (Null check operator used on a null value) because it requires loading

Viewed 60

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],`


`),`
`);`
`}));`
1 Answers

I am following the same course. I will search the solution in the Discord server and I found it:

Change the code in user_provider.dart like this:

class UserProvider with ChangeNotifier {
  User? _user;
  final AuthMethods _authMethods = AuthMethods();

  User? get getUser => _user;

  Future<void> refreshUser() async {
    User user = await _authMethods.getUserDetails();
    _user = user;
    notifyListeners();
  }
}
And this is the code for add_post_screen.dart:
@override
  Widget build(BuildContext context) {
    final User? user = Provider.of<UserProvider>(context).getUser;
    return user == null
        ? const Center(
            child: CircularProgressIndicator(
            color: Colors.black,
          ))
        : _file == null
            ? Center(
                child: IconButton(
                  icon: const Icon(Icons.upload),
                  onPressed: () => _selectImage(context),
                ),
              )
            : Scaffold(
                appBar: AppBar(
                  backgroundColor: Colors.grey,
                  leading: IconButton(
                      icon: const Icon(Icons.arrow_back), onPressed: () {}),

Following that you have to change your user_provider.dart as follows:

class UserProvider with ChangeNotifier {
  User? _user;
  final AuthMethods _authMethods = AuthMethods();

  // Change it
  User? get getUser => _user;

  // update the value of an user
  Future<void> refreshUser() async {
    User user = await _authMethods.getUserDetails();
    _user = user;
    notifyListeners();
  }
}

And in your post_card.dart you have to add a verification when the user == null, if it's null you have to put a CircularProgressIndicator, else the next of your code that show all the posts:

@override
  Widget build(BuildContext context) {
    final model.User? user = Provider.of<UserProvider>(context).getUser;

    /*Fixing error related with null value located in user provider*/
    return user == null
        ? const Center(
            child: CircularProgressIndicator(
              color: Colors.white,
            ),
          )
        : Container(//the rest of your code)
Related