This question already has answers here:
The getter 'documents' isn't defined for the type 'QuerySnapshot'
(4 answers)
The getter 'documents' isn't defined for the type 'QuerySnapshot<Object>'
(1 answer)
The getter 'documents' isn't defined for the type 'QuerySnapshot<Object?>' [duplicate]
(1 answer)
Closed last month.
I want to query Firestore for a certain record where the field that is a List contains two specific elements (order doesn't matter).
However, the error says that I cannot return the value from this query. Specifically the getter 'documents' is not available for a map. Ironically, I created the map because the I needed to return a list which is not a Snapshot.
This is my code:
import 'package:cloud_firestore/cloud_firestore.dart';
Future<List<ChatsRecord>> getChatDoc( DocumentReference chatUserRef, DocumentReference authUserRef, ) async { // Add your function code here!
final firestore =
FirebaseFirestore.instance; // Get a reference to the Firestore database final collectionRef =
firestore.collection('chats'); // Get a reference to the collection final filteredDocuments = collectionRef.where('users', isEqualTo: [
authUserRef,
chatUserRef ]); // Use the `where` method to filter the list of documents
final queryDocuments = await filteredDocuments
.get(); // You can then use the get method on this Query object to retrieve the list of documents.
List<ChatsRecord> listChatDocs = [];
for (DocumentSnapshot doc in queryDocuments.documents) {
ChatsRecord chatDoc = ChatsRecord.fromMap(doc.data);
listChatDocs.add(chatDoc); // add chatDoc
}
return listChatDocs; }
Here is the error:
Error: Command failed: flutter build web --web-renderer html --no-pub --no-version-check
Target dart2js failed: Exception:
lib/custom_code/actions/get_chat_doc.dart:34:39:
Error: Member not found: 'ChatsRecord.fromMap'.
ChatsRecord chatDoc = ChatsRecord.fromMap(doc.data);
^^^^^^^
lib/custom_code/actions/get_chat_doc.dart:33:47:
Error: The getter 'documents' isn't defined for the class 'QuerySnapshot<Map<String, dynamic>>'.
- 'QuerySnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/root/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-4.2.0/lib/cloud_firestore.dart').
- 'Map' is from 'dart:core'.
for (DocumentSnapshot doc in queryDocuments.documents) {
^^^^^^^^^
Error: Compilation failed.
I think is queryDocuments.docs not queryDocuments.documents
Related
Learning Dart and using dart_code_metrics to ensure that I write code that meets expectations. One of the rules that is active is avoid-non-null-assertion.
Note, the code below was created to recreate the problem encountered in a larger code base where the value of unitString is taken from a JSON file. As such the program cannot control what is specified in the JSON file.
From pubspec.yaml
environment:
sdk: '>=2.15.0 <3.0.0'
// ignore_for_file: avoid_print
import 'package:qty/qty.dart';
void main() {
const String unitString = 'in';
// unit.Width returns null if unitString is not a unit of Length.
if (Length().unitWith(symbol: unitString) == null) {
print('units $unitString not supported.');
} else {
// The following line triggers avoid-non-null-assertion with the use of !.
final Unit<Length> units = Length().unitWith(symbol: unitString)!;
final qty = Quantity(amount: 0.0, unit: units);
print('Qty = $qty');
}
}
If I don't use ! then I get the following type error:
A value of type 'Unit<Length>?' can't be assigned to a variable of type 'Unit<Length>'.
Try changing the type of the variable, or casting the right-hand type to 'Unit<Length>'.
Casting the right-hand side to
Unit<Length>
fixes the above error but cause a new error when instantiating Quantity() since the constructor expects
Unit<Length>
and not
Unit<Length>?
I assume there is an solution but I'm new to Dart and cannot formulate the correct search query to find the answer.
How can I modify the sample code to make Dart and dart_code_metrics happy?
Your idea of checking for null before using a value is good, it's just not implemented correctly. Dart does automatically promote nullable types to non-null ones when you check for null with an if, but in this case you need to use a temporary variable.
void main() {
const String unitString = 'in';
//Use a temp variable, you could specify the type instead of using just using final
final temp = Length().unitWith(symbol: unitString);
if (temp == null) {
print('units $unitString not supported.');
} else {
final Unit<Length> units = temp;
final qty = Quantity(amount: 0.0, unit: units);
print('Qty = $qty');
}
}
The basic reason for that when you call your unitWith function and see that it's not null the first time, there's no guarantee that the when you call it again that it will still return a non-null value. I think there's another SO question that details this better, but I can't seem to find.
Goal:
I want to search for a Firestore record (there should be only one) where the value of the List field contains two elements that are input as parameters: Document References to the User collection.
However, I don't understand how to fix the error given my approach to searching for a given document.
This is my code:
import 'package:cloud_firestore/cloud_firestore.dart';
Future<ChatsRecord> getChatDoc(
DocumentReference chatUserRef,
DocumentReference authUserRef,
) async {
// Add your function code here!
final firestore =
FirebaseFirestore.instance; // Get a reference to the Firestore database
final collectionRef =
firestore.collection('chats'); // Get a reference to the collection
final filteredDocuments = collectionRef.where('fieldName', isEqualTo: [
authUserRef,
chatUserRef
]); // Use the `where` method to filter the list of documents
final documents = await filteredDocuments
.get(); // You can then use the get method on this Query object to retrieve the list of documents.
return documents;
}
This is my error:
Error: Command failed: flutter build web --web-renderer html --no-pub --no-version-check
Target dart2js failed: Exception:
lib/custom_code/actions/get_chat_doc.dart:31:10:
Error: A value of type 'QuerySnapshot<Map<String, dynamic>>' can't be returned from an async function with return type 'Future<ChatsRecord>'.
- 'QuerySnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/root/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-4.2.0/lib/cloud_firestore.dart').
- 'Map' is from 'dart:core'.
- 'Future' is from 'dart:async'.
- 'ChatsRecord' is from 'package:counter_party/backend/schema/chats_record.dart' ('lib/backend/schema/chats_record.dart').
return documents;
^
Error: Compilation failed.
Exception: Failed to compile application for the Web.
void main() {
List a = ["aa", "bbb", "ccccc"];
Iterable b = a.iterator;
while (b.moveNext()) {
/*The method 'moveNext' isn't defined for the type 'Iterable'.
Try correcting the name to the name of an existing method,
or defining a method named 'moveNext'.dartundefined_method
*/
print(b.current);
/* The getter 'current' isn't defined for the type 'Iterable<dynamic>'.
Try importing the library that defines 'current',
correcting the name to the name of an existing getter,
or defining a getter or field named 'current'.dartundefined_getter
*/
}
}
Multiple problems. E.g. a.iterator does not return a Iterable but instead Iterator. Also, you should not write List since that means List<dynamic> in Dart which means you are loosing type information.
The easiest way is to just use final or var and let Dart automatically use the most precise type (e.g. List<String>):
void main() {
final a = ["aa", "bbb", "ccccc"];
final b = a.iterator;
while (b.moveNext()) {
print(b.current);
}
}
So I am having trouble understanding what the issue is here. I am trying to create a linked-list that can hold a String and a List object. This is what I have so far:
import 'dart:collection';
void main() {
var entry = LinkedList<MyLinkedListEntry<String,List>>();
var list1 = List();
entry.addFirst('First', list1);
}
class MyLinkedListEntry<T,U> extends LinkedListEntry< MyLinkedListEntry<T,U>>{
T date;
U array;
MyLinkedListEntry(this.date,this.array);
}
The problem I am having is that I keep getting an error saying I have too many positional arguments or the argument type ‘String’ can’t be assigned to the parameter type 'MyLinkedListEntry'.
You are creating a linked list where the primary type is MyLinkedListEntry. That means you can only add items of that type to the list (or types that are convertible to it):
void main() {
var entry = LinkedList<MyLinkedListEntry<String,List>>();
var list1 = List();
entry.addFirst(MyLinkedListEntry('First', list1));
}
I need help with an error I get while running a flutter project. I made a clone of
https://github.com/miketraverso/devfestapp.
After getting all the packages updated - I got the following error: Compiler message:
lib/views/scheduled_session_widget.dart:51:34: Error: The method '[]' isn't defined for the class 'dart.core::int'.
Try correcting the name to the name of an existing method, or defining a method named '[]'.
mSessions[sessionIter['0'].toString()]; // ignore: undefined_operator
Could someone explain what I could do to solve this error ?
It seems that sessions is collection of ints: List<int> sessions; in class TimeSlot, and later you have forEach on it so you can replace that code with:
timeSlot.sessions.forEach((sessionIter) {
Session session = mSessions[sessionIter.toString()];
if (session != null) {
Widget sessionCard = buildSessionCard(timeSlot, session);
sessionCards.add(sessionCard);
}
});