Home > other >  Listview Firebase Flutter The method '[]' can't be unconditionally invoked because th
Listview Firebase Flutter The method '[]' can't be unconditionally invoked because th

Time:03-21

I want to show data from firebase in a listView

I just want to show the id of my collection

but I have problem with title: doc.data()["id"],

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class DemandeList extends StatelessWidget {
  final db = FirebaseFirestore.instance;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Demandes listes"),
        centerTitle: true,
      ),
      body: StreamBuilder<QuerySnapshot>(
        stream: db.collection('demande').snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return Center(
              child: CircularProgressIndicator(),
            );
          } else
            return ListView(
              children: snapshot.data!.docs.map((doc) {
                return Card(
                  child: ListTile(
                     title: doc.data()["id"],
                  ),
                );
              }).toList(),
            );
        },
      ),
    );
  }
}

CodePudding user response:

Try

doc.id;
// Or doc.Id;

Instead of

doc.data()['id'];

CodePudding user response:

The problem is related to a flutter update.

In the newest flutter update, there is no need in adding '.data()'.

Removing the '.data()' from the code in the description solves the issue.

  • Related