Reputation: 339
I am trying to build a dropdown that is filled with values from the database.
The error happens at
items: machineList[index].serienummer((String items)
The error I get is:
The function can't be unconditionally invoked because it can be null.
I tried to add items: machineList[index].serienummer!
or items: machineList[index].serienummer?
and items: machineList[index].serienummer??
parameters but doesn't seem to solve it. Any help would be great.
This is my dart file
class _MachinepageState extends State<Machinepage> {
_MachinepageState();
final ApiService api = ApiService();
late List<Machine> machineList;
String dropdownvalue = "";
@override initState(){
super.initState();
_getMachine();
machineList = [];
}
void _getMachine() async{
machineList = (await ApiService().getMoreMachine(widget.klant.klantId.toString()));
Future.delayed(const Duration(seconds: 1)).then((value) => setState(() {}));
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(title: const Text('Menu'),
backgroundColor:Colors.deepOrange
),
body: machineList == null || machineList.isEmpty
? const Center(
child: CircularProgressIndicator(),
)
: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget> [
ListView.builder(
shrinkWrap: true,
itemCount: machineList == null? 0 : machineList.length,
itemBuilder: (BuildContext context, int index) => Row(
children: [
Container(
margin: const EdgeInsets.only(top: 20.0, bottom: 25.0),
child: DropdownButton(
// Initial Value
value: machineList[1].serienummer,
// Down Arrow Icon
icon: const Icon(Icons.keyboard_arrow_down),
// Array list of items
items: machineList[index].serienummer((String items) {
return DropdownMenuItem(
value: items,
child: Text(items),
);
}).toList(),
// After selecting the desired option, it will
// change button value to selected value
onChanged: (String? newValue) {
setState(() {
dropdownvalue = newValue!;
});
},
),
)
],
)
),
My Machinemodel.dart
import 'dart:convert';
import 'package:flutter/src/material/dropdown.dart';
List<Machine> welcomeFromJson(String str) => List<Machine>.from(json.decode(str).map((x) => Machine.fromJson(x)));
String welcomeToJson(List<Machine> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Machine {
Machine({
this.serienummerId,
this.serienummer,
this.bouwjaar,
this.urenstand,
this.locatie,
this.klantId,
});
int? serienummerId;
String? serienummer;
String? bouwjaar;
String? urenstand;
String? locatie;
String? klantId;
factory Machine.fromJson(Map<String, dynamic> json) => Machine(
serienummerId: json["SerienummerId"],
serienummer: json["Serienummer"],
bouwjaar: json["Bouwjaar"],
urenstand: json["Urenstand"],
locatie: json["Locatie"],
klantId: json["KlantId"],
);
Map<String, dynamic> toJson() => {
"SerienummerId": serienummerId,
"Serienummer": serienummer,
"Bouwjaar": bouwjaar,
"Urenstand": urenstand,
"Locatie": locatie,
"KlantId": klantId,
};
}
Upvotes: 1
Views: 189
Reputation: 271
Since you passed the machineList[1].serienummer
in DropdownButton value
, I'm assuming that serienummer
is not a list but some kind of value, i.e., an integer or string.
And you're trying to do this items: machineList[index].serienummer((String items)
which will not work as items
will only take lists as value.
So try this this:
items: machineList!.map((item) {
return DropdownMenuItem(
value: item.serienummer, //or whatever value you want to pass to the item
child: Text(item.serienummer.toString()),
);
})!.toList(),
Don't forget to put the null and empty check for machineList
before passing it in DropDownButton items
Upvotes: 2