Search code examples
flutterfirebasegoogle-cloud-firestoreflutter-listviewflutter-list-tile

Flutter firebase: Method "??"can't be unconditionally invoked


I'm trying to create list of users with Flutter Web. The list is ready. But I want that when I click on the listTile it should show the details of that particular listTile in a container next to the list. Can someone tell me what I did wrong here?

code:

class AllUserScreen extends StatefulWidget {
  const AllUserScreen({super.key});

  @override
  State<AllUserScreen> createState() => _AllUserScreenState();
}

class _AllUserScreenState extends State<AllUserScreen> {
  Map<String, dynamic>? selectedUser;

  @override
  Widget build(BuildContext context) {
    final screenHeight = MediaQuery.of(context).size.height;
    final screenWidth = MediaQuery.of(context).size.width;

    return Scaffold(
        // extendBodyBehindAppBar: true,
        appBar: AppBar(
          centerTitle: true,
          title: const Text(
            'All Users',
            textAlign: TextAlign.center,
            style: TextStyle(fontSize: 30, color: Colors.green),
          ),
          backgroundColor: Colors.transparent,
          elevation: 0,
          leading: IconButton(
              onPressed: () {
                Navigator.pop(context);
              },
              icon: const Icon(
                Icons.arrow_back_ios,
                color: Colors.green,
              )),
        ),
        body: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            SizedBox(
              width: screenWidth * 0.4,
              child: StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
                // inside the <> you enter the type of your stream
                stream:
                    FirebaseFirestore.instance.collection('users').snapshots(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return ListView.builder(
                      itemCount: snapshot.data!.docs.length,
                      itemBuilder: (context, index) {
                        return Card(
                          child: ListTile(
                            onTap: () {
                              setState(() {
                                selectedUser = snapshot.data!.docs[index]
                                    as Map<String, dynamic>?;
                              });
                            },
                            title: Text(
                              snapshot.data!.docs[index].get('Full name'),
                            ),
                          ),
                        );
                      },
                    );
                  }
                  if (snapshot.hasError) {
                    return const Text('Error');
                  } else {
                    return const Center(child: CircularProgressIndicator());
                  }
                },
              ),
            ),
            SizedBox(
              width: screenWidth * 0.03,
            ),
            Container(
              width: screenWidth * 0.4,
              child: Column(
                children: [Text(selectedUser['Full name']?? '')],
              ),
            )
          ],
        ));
  }
}

Error screen

Please can someone tell me what I did wrong


Solution

  • Change the Text to

    Text(selectedUser?['Full name']?? '')