Reputation: 43
I have a two error for this Flutter apps.
The argument type 'Object?' can't be assigned to the parameter type 'List'.
The property 'length' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.') or adding a null check to the target ('!').
When I add ('!') or null check, new error showed up.
The getter 'length' isn't defined for the type 'Object'. Try importing the library that defines 'length', correcting the name to the name of an existing getter, or defining a getter or field named 'length'.
Here's the code :
list() {
return Expanded(
child: FutureBuilder(
future: siswa,
builder: (context, snapshot) {
if (snapshot.hasData) {
return dataTable(snapshot.data);
}
if (null == snapshot.data || snapshot.data!.length == 0) {
return const Text("");
}
return const CircularProgressIndicator();
},
),
);
}
And here's the full code on my GitHub : https://github.com/kisekifrans/pencatat_nama_mahasiswa
Thank you for the attention and have a wonderful day everyone.
Upvotes: 0
Views: 2987
Reputation: 91
Your snapshot.data
type is not defined, so it is taken as an Object?
. This causes the errors.
What can you do:
(snapshot.data as List<Siswa>).length
<type>
the end of your FutureBuilder
. In your case it will be like FutureBuilder<List<Siswa>>
and so your snapshot.data
will be identified as ListYou can use such cast in many different cases, like casting types of your iterable:
List<String>
can contain only String
valuesMap<String, dynamic>
can only get String
values as key, but the value can implement any type (that is what dynamic
means)More about casting you can read here https://itnext.io/flutter-methodchannel-dart-generic-and-type-casting-54ca48e6d3ad
Okay, lets look firstly on src/siswa.dart
:
class Siswa {
late int id;
late String nama;
late int nim;
Siswa(this.id, this.nama, this.nim);
Map<String, dynamic> toMap() {
var map = <String, dynamic>{
'id': id,
'nama': nama,
'nim': nim,
};
return map;
}
Siswa.fromMap(Map<String, dynamic> map) {
id = map['id'];
nama = map['nama'];
nim = map['nim'];
}
}
All it's parameters has type and can not be null. Look at main.dart
at line 64:
Siswa e = Siswa(null, nama, nim);
You apply null
to int
type. You can:
id
field nullable (late int? id;
)int
instead of null
(Siswa e = Siswa(0, nama, nim);
)Text(siswa.nim),
Here you trying to print int
through Text
widget. But Text
widget applies only String
type, so you should do like this:
Text(siswa.nim.toString()),
Upvotes: 1