I’m developing a Flutter application where I have a ContactsPage that fetches user profiles from Firestore and allows navigation to a ChatPage for each contact. I want to display the userName and userImageLink in the AppBar of ChatPage just like WhatsApp does.
Here’s the relevant code structure:
ContactsPage (contacts_page.dart):
// Imports and class definition...
void navigateToIndividualPage(Contact contact) {
Navigator.pushNamed(
context,
ChatPage.screenRoute,
arguments: {
'name': contact.displayName ?? 'No name',
'userImageLink': (doc.data() as Map<String, dynamic>?)?['image_link'] ?? '',
// Other necessary user data
},
);
}
ChatPage (chat_page.dart):
import 'package:flutter/material.dart';
class ChatPage extends StatelessWidget {
static const String screenRoute = "ChatPage";
@override
Widget build(BuildContext context) {
final args = ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
final userName = args['name'];
final userImageLink = args['userImageLink'];
return Scaffold(
appBar: AppBar(
leading: CircleAvatar(
backgroundImage: NetworkImage(userImageLink),
),
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(userName),
Text(
'Last seen',
style: TextStyle(fontSize: 12),
),
],
),
),
body: Center(
// Placeholder for chat UI
),
);
}
}
Issue:
When navigating to ChatPage from ContactsPage, I encounter a TypeError stating type ‘Null’ is not a subtype of type ‘String’ on the line final userName = args[‘name’];. This suggests that args[‘name’] or args[‘userImageLink’] might be null or missing in some cases.
What I’ve Tried:
Checked that args are correctly passed from ContactsPage using Navigator.pushNamed.
Ensured that userName and userImageLink are correctly accessed from args in ChatPage.
Expected Outcome:
I expect the userName and userImageLink obtained from ContactsPage to be displayed in the AppBar of ChatPage, allowing users to see the contact’s name and profile image.
Any help on handling null values in args or alternative approaches to achieve this functionality would be appreciated. Thank you!
Hussien990 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.