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

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

Related

The parameter 'abc' can't have a value of 'null' because of its type, but the implicit default value is 'null'

#required to a property gave such ERROR:
The parameter 'lng' can't have a value of 'null' because of its type, but the implicit default value is 'null'. (Documentation)
but removing #, removes the error. how ?
I mean, the value still can be null. What does it have to do with "#" symbol.
see pictures:
You dont need to use # anymore, just use required.
({
required this.lat,
required this.lng,
})
if you like to accept null data, use DataType? varName.
String? lat;
String? lng;
More about understanding-null-safety

How to pass boolean expression to variable parameter of QueryOptions of GraphQL package in flutter?

#gql #flutter Hi everyone, I am using graphQL with flutter and would like to pass boolean expression to my graphQL query.
I am using the QueryOption object type of the graphQL flutter package to pass the query to graphql and to define my query parameters.
The problem I have is that I don't know how to tell flutter that I am passing a boolean expression to the variable parameter of the QueryOption object. Is there a way to do that?
Future<QueryResult> FilteredQuery(
final GraphQLClient client,
final FilterModel filter,
final bool isAvailable,
) async {
final QueryOptions options = QueryOptions(
document: gql(queryCreationsFiltered),
variables: <String, dynamic>{
'condition1': isAvailable== false ? {false}:{is_available: {_eq: true}}
}
Usually to be able to match the right type of the query variable I pass the associated dart type in flutter!
The idea behind that is to apply conditional filtering.
Any help will be welcomed!
Thanks :slight_smile:
I managed to solve my problem.
The syntax to use to express the boolean expression in dart should be the following.
{"is_available": {"_eq": true}} and not {is_available: {_eq: true}}
My only remaining question is how to easily pass a boolean expression that evaluates to false {false} doesnt' work!

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?

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

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');
});