Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - How set all my data (fields) from firebase documents to table

I'm trying to get all user data fields from documents to table to compare users scores live.

This is my Firestore document:

enter image description here

right now the only thing I success to get is only the Collection Id and the documents files name(the uuid). any idea how to get user data fields?

this is what I've built for now:

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

class Records extends StatefulWidget {
  @override
  _RecordsState createState() => _RecordsState();
}

@override
class _RecordsState extends State<Records> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: new Scaffold(
        appBar: AppBar(
            title: Text(" טבלת שיאים לשאלון מספר - " + IDController.text)),
        body: new Stack(
          fit: StackFit.expand,
          children: [
            new Image(
              image: AssetImage(
                "assets/background/zooc.jpg",
              ),
              fit: BoxFit.cover,
              height: double.infinity,
              width: double.infinity,
              alignment: Alignment.center,
            ),
            new StreamBuilder(
              stream: FirebaseFirestore.instance
                  .collection(IDController.text)
                  .orderBy('studentScore', descending: true)
                  .snapshots(),
              builder: (context, snapshot) {
                if (snapshot.hasError) return Text('Error = ${snapshot.error}');
                if (snapshot.hasData) return new Text('המערכת טוענת נתונים...');
                return new DataTable(
                  columns: <DataColumn>[
                    new DataColumn(label: Text('studentName')),
                    new DataColumn(label: Text('studentScore')),
                    new DataColumn(label: Text('endTime')),
                  ],
                  rows: _createRows(snapshot.data),
                );
              },
            ),
          ],
        ),
      ),
    );
  }

  List _createRows(QuerySnapshot snapshot) {
    List newList = snapshot.docs.map(
      (DocumentSnapshot documentSnapshot) {
        return new DataRow(
          cells: [
            DataCell(Text(documentSnapshot['studentName'].toString())),
            DataCell(Text(documentSnapshot['studentScore'].toString())),
            DataCell(Text(documentSnapshot['endTime'].toString())),
          ],
        );
      },
    ).toList();
    return newList;
  }
}

Thanks, Kobi

like image 490
kobi077 Avatar asked May 05 '26 07:05

kobi077


1 Answers

Your mistake is using multiple "orderby" which are not allowed. Try with any one like -

FirebaseFirestore.instance
                  .collection(IDController.text)
                  .orderBy('studentScore', descending: true)
                  .snapshots()

Or if you want to sort by all then perform the sorting separately after receiving the data.

Hope it helps....

like image 153
Nikulsinh Sodha Avatar answered May 06 '26 21:05

Nikulsinh Sodha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!