The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'. Dart Issue in Grouping the List according to date - flutter

I am trying to group the API data according to date.
But I am getting this error:
The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'.
The error is on this line:
groupBy(prayerDetails, (obj) => obj['msgdate'].toString());
How can I resolve this error?
This is my code:
var groupByDate =
groupBy(prayerDetails, (obj) => obj!(obj as Object)['msgdate'].toString());
groupByDate.forEach((date, list) {
print('Grouped Date');
print('$date:');
list.forEach((listItem) {
print('${listItem["msgdate"]}, ${listItem["message"]}');
});
print('\n');
});

Related

Error: The operrator '[]' isn't defined for the class 'Object?' [duplicate]

This question already has answers here:
how to fix the "The operator '[]' isn't defined for the type 'Object'" error while getting data from snapshot? [duplicate]
(1 answer)
Firebase Firestore Error: The operator '[]' isn't defined for the class 'Object'
(4 answers)
Closed 2 days ago.
I saw that there in another post about the same error but it was solved by just adding () next to the data keyword, I tried it but still showing error.
It is possible to get null data. You can do:
.distanceBetween(
…,
…,
double.tryParse("${document.data()?['latitude']}") ?? 0,
double.tryParse("${document.data()?['longitude']}") ?? 0, // Just to be sure on parser
);

Flutter Firebase whereContains two values [duplicate]

This question already has answers here:
Firestore search array contains for multiple values
(6 answers)
Closed last month.
Here is my code. I have two input parameters authUser and chatUser. I have a record called Chats, with a List field called users. I want to query and get the document where the List field users contains BOTH authUser and chatUser.
import 'package:cloud_firestore/cloud_firestore.dart';
Future<ChatsRecord> getChatDocFromChatUserAuthUser(
DocumentReference? chatUserRef,
DocumentReference? authUserRef,
) async {
// Add your function code here!
ChatsRecord chatDoc = await FirebaseFirestore.instance
.collection('chats')
.where("users", arrayContains: chatUserRef)
.where("users", arrayContains: authUser)
.get()
.then((snapshot));
return chatDoc;
}
Here is the error I get from trying the solution below:
lib/custom_code/actions/get_chat_doc.dart:23:34:
Error: A value of type 'List<QueryDocumentSnapshot<Object?>>' can't be returned from an async function with return type 'Future<List<ChatsRecord>>'.
- 'List' is from 'dart:core'.
- 'QueryDocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/root/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-4.2.0/lib/cloud_firestore.dart').
- 'Object' 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 (await snapshots.first).toList();
^
Error: Compilation failed.
You should try using conditions from QuerySnapshots by :
CollectionReference chatDoc = Firestore.instance.collection('chats');
final snapshots = chatDoc.snapshots().map((snapshot) => snapshot.documents.where((doc) => doc["users"] == chatUserRef || doc["users"] == authUser));
return (await snapshots.first).toList();

flutter dart datetime in list<map> occur error

my code is below. I want it to be type-recognized as a datetime type at compile time.
var myList = [{
'message': 'foo',
'time': DateTime.now()
}];
DateTime.now().difference(myList[0]['time']);
and it has error of The argument type 'Object?' can't be assigned to the parameter type 'DateTime'.
how can i fix this?
You need to add a type cast for this with the as keyword:
DateTime.now().difference(myList[0]['time'] as DateTime)

The operator '[]' isn't defined for the class 'Object' after migrating to null safety [duplicate]

This question already has answers here:
The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'
(10 answers)
The operator '[]' isn't defined for the class 'Object?'
(1 answer)
Closed 1 year ago.
I migrated my code to null safety but now I get an issue:
When I want to fetch a document from firestore I get this error:
Error: The operator '[]' isn't defined for the class 'Object'.
Here is the code snippet where I want to call ['plans'] on the snapshot data.
body: FutureBuilder(
future: handler.getPlans(),
builder: (context, snapshot) {
if (snapshot.hasData) {
final plans = snapshot.data!['plans']; /// here I cant call ['plans']
Before null safety this worked. Whats the issue?

Flutter: the operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'

How can I get the nested object values? print(_user.userLocation()['location']); returns {country: Eesti, city: Tallin}
So, I tried with _user.userLocation()['location']['city'] to get the value Tallinn. However, I am getting
The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'.
print(_user.userLocation()) and print(_user.location().runtimeType); returns
location: {
country: Eesti,
city: Tallinn,
}
flutter: _InternalLinkedHashMap<String, dynamic>
I tried to set a variable to var a = _user.location()['location'] and then a['city']. However, this is not working as well.
You should try like this,
var a = _user.userLocation() as Map;
print(a['location]['city']);
I issue here is dart's type inference is not able to identify the type automatically.So that it gives the error.But the above solution should work