how to retrieve a list from a document in firebase flutter?

389

Use get(), to get a specific field in your user map, like this:

child: Text(snapshot.data!.docs[index].get('Cart'),
Share:
389
Muhammad Rasul
Author by

Muhammad Rasul

Updated on December 31, 2022

Comments

  • Muhammad Rasul
    Muhammad Rasul over 1 year

    i want to retrieve a list inside a document and show it through a listview.builder

    Cart:[ 0 "default", 1 "bugatti chiron", 2 "examp" 3 "bugatti veron", 4 "examp", 5 "bugatti chiron 20" ]

    so the above array is what i want to retrieve and show in a listview.

    and the class below is how i tried to implement it but failed

    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:firebase_auth/firebase_auth.dart';
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    class cart extends StatefulWidget {
      @override
      _cartState createState() => _cartState();
    }
    
    class _cartState extends State<cart> {
      FirebaseAuth auth = FirebaseAuth.instance;
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(),
          body: StreamBuilder<QuerySnapshot>(
            stream: FirebaseFirestore.instance.collection('user').snapshots(),
            builder: (context, snapshot) {
              return Column(children: [
                snapshot.hasData
                    ? Expanded(
                        child: ListView.builder(
                        itemBuilder: (BuildContext context, int index) {
                          return Column(children: [
                            InkWell(
                                onTap: () {},
                                child: Card(
                                  child: Text(snapshot.data!.docs[index][Cart]),
                                )),
                          ]);
                        },
                        itemCount: snapshot.data!.docs.length,
                      ))
                    : Center(
                        child: CupertinoActivityIndicator(),
                      ),
                TextButton(
                    child: Text('checkout'),
                    onPressed: () {
                      FirebaseFirestore.instance
                          .collection('user')
                          .doc(auth.currentUser!.uid);
                    })
              ]);
            },
          ),
        );
      }
    }
    
    • Victor Eronmosele
      Victor Eronmosele over 2 years
      Hi, please post your Firestore database structure and also when you say "and the class below is how i tried to implement it but failed", what exactly failed?
    • Muhammad Rasul
      Muhammad Rasul over 2 years
      well its: user > XE4nqk1hVnYlDjn2Y5cDMQ3cJdi1 > email "[email protected]" name "Moe" password "11223344" uid "XE4nqk1hVnYlDjn2Y5cDMQ3cJdi1" Cart 0 "default" 1 "ex" 2 "bugatti veron" 3 "ex" 4 "bugatti chiron 20" 5 "bugatti chiron 40"
  • Muhammad Rasul
    Muhammad Rasul over 2 years
    it works when i specify field's index like: child: Text(snapshot.data!.docs[index].get('Cart')[0], still it managed to retrieve the first index of all the users, while i want all the data of the list in the current active user!!?
  • Huthaifa Muayyad
    Huthaifa Muayyad over 2 years
    You need another listview builder for that.