amirudev
amirudev

Reputation: 83

The operator '[]' isn't defined for the class 'Object?', its list not an object, why dart says its an object?

var jobs = [
    {
      'id': 1,
      'name': 'Unemployment',
      'salary': '0',
      'skills': ['Math', 'Social'],
    },
];

I want to get first value of skills object inside first jobs list, but i've got

The method '[]' can't be unconditionally invoked because the receiver can be 'null'. (view docs) Try making the call conditional (using '?.') or adding a null check to the target ('!')

is it an object ? because every time I'm running print(jobs[0]['skills'][0]); it always return

Error: The operator '[]' isn't defined for the class 'Object?'.

  • 'Object' is from 'dart:core'. print(jobs[1]['skills'][0]); ^ Error: Compilation failed.

Upvotes: 0

Views: 107

Answers (2)

Tasnuva Tavasum oshin
Tasnuva Tavasum oshin

Reputation: 4750

List<dynamic> jobs = <dynamic>[];

 jobs = [
    {
      'id': 1,
      'name': 'Unemployment',
      'salary': '0',
      'skills': ['Math', 'Social'],
    },
];

Now Print :

print(jobs[0]['skills'][0]);

and it should print Math now

Upvotes: 1

xion
xion

Reputation: 1369

As if you know the skills would be an array, you can use typecast (the "as" operator) it to a List as below:

print((jobs[0]['skills'] as List<String>).toList().first);
// Math

Upvotes: 1

Related Questions