Perry
Perry

Reputation: 72

Flutter : NoSuchMethodError (NoSuchMethodError: The getter 'isEmpty' was called on null Receiver: null Tried calling: isEmpty)

I am displaying all users from firebase in a listview.builder on my message screen, when I click on a user in the list I want to be sent to the chat screen which displays that users profile image and name in the app bar.

The code pauses on this exception:

backgroundImage: widget.receiver.profileImageUrl.isEmpty
                          ? AssetImage(
                              'assets/images/default_profile_picture.png')
                          : CachedNetworkImageProvider(
                              widget.receiver.profileImageUrl)),
                  child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Text(
                          widget.receiver.name

Relevant Listview:

body: ListView.builder(
          itemCount: usersList.length,
          itemBuilder: ((context, index) {
            return Padding(
              padding: const EdgeInsets.all(4.0),
              child: InkWell(
                onTap: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: ((context) => ChatScreen(
                                profileImageUrl: usersList[index].profileImageUrl,
                                name: usersList[index].name,
                                receiverUid: usersList[index].uid,
                                // receiverUid: widget.currentUserId,
                                userId: widget.currentUserId,
                                receiver: user,
                              )
                              )
                              ));
                },
                child: ListTile(
                  leading: CircleAvatar(
                      backgroundImage: usersList[index].profileImageUrl.isEmpty
                          ? AssetImage(
                              'assets/images/default_profile_picture.png')
                          : CachedNetworkImageProvider(
                              usersList[index].profileImageUrl)),
                  title: Text(usersList[index].name),
                ),
              ),
            );
          }),
        )

Chat screen constructor:

class ChatScreen extends StatefulWidget {
  final String profileImageUrl;
  final String name;
  final String receiverUid;
  final User sender;
  final User receiver;
  final String userId;

  ChatScreen(
      {this.profileImageUrl,
      this.name,
      this.receiverUid,
      this.sender,
      this.receiver,
      this.userId});

  @override
  _ChatScreenState createState() => _ChatScreenState();
}

Chat screen initstate:

@override
      void initState() {
       super.initState();
        _repository.getCurrentUser().then((user) {
         _currentUserId = user.uid;
       setState(() {
        sender = User(
            id: user.uid,
            name: user.displayName,
            profileImageUrl: user.photoUrl);
      });
    });
    _repository.getUserWithId(widget.userId).then((user) {
      setState(() {
        receiver = User(
            id: user.id,
            profileImageUrl: user.profileImageUrl,
            name: user.name);
      });
    });
  }

Messages screen constructor:

class MessagesScreen extends StatefulWidget {
  final String userId;
  final User receiver;
  final String currentUserId;
  final String receiverUid;

  MessagesScreen(
      {this.userId, this.receiver, this.currentUserId, this.receiverUid});
  @override
  _MessagesScreenState createState() => _MessagesScreenState();
}



Upvotes: 0

Views: 1022

Answers (1)

Haroon Ashraf Awan
Haroon Ashraf Awan

Reputation: 1219

That's because profileImageUrl in some of your users is null.Add a null check before .isEmpty using ? operator.It checks for null values.

 backgroundImage: (widget.receiver.profileImageUrl?.isEmpty ?? true)
                              ? AssetImage(
                                  'assets/images/default_profile_picture.png')
                              : CachedNetworkImageProvider(
                                  widget.receiver.profileImageUrl)),
                      child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Text(
                              widget.receiver.name

Upvotes: 1

Related Questions