Home > OS >  Flutter - The operator '[]' isn't defined for the type 'Object'. - record n
Flutter - The operator '[]' isn't defined for the type 'Object'. - record n

Time:11-04

I am trying to do a compound query. If this one works, then I want to display the results in a listView.builder. The records are not displayed properly. I do not understand why. I must display the name field of each document find using the query. I have tried different options, but the result is also an error. Thank you.


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

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

  @override
 State<LoadDataFromFirestore> createState() => _LoadDataFromFirestoreState();
}

class _LoadDataFromFirestoreState extends State<LoadDataFromFirestore> {

  QuerySnapshot? querySnapshot;

  @override
  void initState() {
    super.initState();
    myQuery().then((results) {
      setState(() {
        querySnapshot = results;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _showDrivers(),
    );
  }

  Widget _showDrivers() {

    if (querySnapshot != null) {
      return ListView.builder(
        primary: false,
        itemCount: querySnapshot?.docs.length,
        padding: const EdgeInsets.all(12),
        itemBuilder: (context, i) {
          return Column(
            children: <Widget>[

              Text(querySnapshot!.docs[i].data().toString()),
            ],
          );
        },
      );
    } else {
      return const Center(
        child: CircularProgressIndicator(),
      );
    }
  }

  Future myQuery () async{
    final uid = FirebaseAuth.instance.currentUser!.uid;
    final path = 'Users/$uid/allTasks';

    final currentQuery = FirebaseFirestore.instance.collection(path);
    Query statusQuery = currentQuery.where('status', isEqualTo: 'Next Action');
    Query importantQuery = statusQuery.where('important', isEqualTo: 'False');

    final snapshot = await importantQuery.get();

    final data = snapshot.docs;
    if(data.isNotEmpty){
      for(var i =0; i < data.length; i  ){
        print(data[i].data());
        return Text(data[i]['name']);
      }
      return data;
    }
  }
}

CodePudding user response:

Since I can't test this, my guess is that the view is not refreshing properly since you used an function to get that widget. Also you're returning an widget (Text) instead of the expected response that is of type QuerySnapshot. Also you're returning data at the end which is a List and not the snapshot.

Go ahead and try this please:

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

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

  @override
 State<LoadDataFromFirestore> createState() => _LoadDataFromFirestoreState();
}

class _LoadDataFromFirestoreState extends State<LoadDataFromFirestore> {

  QuerySnapshot? querySnapshot;

  @override
  void initState() {
    super.initState();
    myQuery().then((results) {
      setState(() {
        querySnapshot = results;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: querySnapshot != null ? ListView.builder(
        primary: false,
        itemCount: querySnapshot?.docs.length,
        padding: const EdgeInsets.all(12),
        itemBuilder: (context, i) {
          return ListTile(
           title: Text(querySnapshot!.docs[i].data().toString()),
          );
        },
      ) : Center(
        child: CircularProgressIndicator(),
      )
    );
  }

  Future myQuery () async{
    final uid = FirebaseAuth.instance.currentUser!.uid;
    final path = 'Users/$uid/allTasks';

    final currentQuery = FirebaseFirestore.instance.collection(path);
    Query statusQuery = currentQuery.where('status', isEqualTo: 'Next Action');
    Query importantQuery = statusQuery.where('important', isEqualTo: 'False');

    final snapshot = await importantQuery.get();

    final data = snapshot.docs;
    if(data.isNotEmpty){
      //for(var i =0; i < data.length; i  ){
      //  print(data[i].data());
      //  return Text(data[i]['name']);
      //}
      return snapshot;
    }
  }
}
  • Related