【发布时间】:2021-11-27 13:26:19
【问题描述】:
I/flutter (8622): NoSuchMethodError: 方法 'map' 在 null 上被调用。 I/颤振(8622):接收器:空 I/flutter (8622): 尝试调用: map(Closure: (dynamic) => Result)
你好,我遇到了这样的错误。 我不知道该怎么办。我尝试了很多解决方案,但没有奏效 对不起我的英语我还在学习颤抖
这是我的代码 对不起我的长代码
这个用户界面屏幕
class JualSampah extends StatefulWidget {
final String? token;
const JualSampah({Key? key, this.token}) : super(key: key);
@override
_JualSampahState createState() => _JualSampahState();
}
int _n = 01;
class Img {
String nama, images;
Img({required this.nama, required this.images});
}
class _JualSampahState extends State<JualSampah> {
final con = HomeController();
String? token;
List qtyList = [];
List totalHarga = [];
List postDetail = [];
List hargaSetor = [];
List namaSampah = [];
List<Result> _listSampah = <Result>[];
List<Result> _listSearch = <Result>[];
var _controller = TextEditingController();
Timer? debounce;
String search = '';
RefreshController _refreshController =
RefreshController(initialRefresh: false);
void _onRefresh() async {
await Future.delayed(Duration(milliseconds: 1000));
_listSampah.clear();
_refreshController.refreshCompleted();
}
void _onLoading() async {
await Future.delayed(Duration(milliseconds: 1000));
_listSampah.clear();
_refreshController.loadComplete();
}
Color setor = Colors.grey;
@override
void initState() {
getToken().then((value) {
con.getSampahId(value);
setState(() {
token = value;
});
});
super.initState();
//! menggunakan qtyList
con.resSampah.listen((value) {
for (var i = 0; i < value.result!.length; i++) {
qtyList.add(0);
postDetail.add(0);
namaSampah.add(0);
hargaSetor.add(0);
totalHarga.add(0);
}
if (mounted)
setState(() {
if (_listSampah.isNotEmpty) {
if (mounted)
setState(() {
_listSampah.clear();
});
} else {
_listSampah.addAll(value.result!);
_listSearch = _listSampah;
}
});
});
}
// @override
// void dispose() {
// super.dispose();
// _listSampah.clear();
// _listSearch.clear();
// }
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Stack(children: [
Container(
height: MediaQuery.of(context).size.height * 0.75,
child: Padding(
padding: const EdgeInsets.only(right: 10, left: 10),
child: ListView(children: [
Container(
padding: EdgeInsets.all(10),
height: 70,
child: Row(
children: [
InkWell(
onTap: () {
Navigator.pop(context);
},
child: Icon(
Icons.arrow_back_ios_new_rounded,
size: 25,
color: Color(0xff85d057),
),
),
SizedBox(
width: 30,
),
Text("Jual Sampah",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
)),
],
)),
Container(
padding: EdgeInsets.all(10),
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.0),
color: Color(0xffF0FFEB)),
child: TextField(
controller: _controller,
decoration: InputDecoration(
border: InputBorder.none,
prefixIcon: Icon(
Icons.search,
color: Color(0xff85d057),
),
hintText: "Search Sampah",
hintStyle:
TextStyle(fontSize: 15, color: Colors.grey[400])),
onChanged: (value) {
_listSampah.where((element) {
var sampah = element.nama!.toLowerCase();
return sampah.contains(value);
}).toList();
},
)),
SizedBox(height: 15),
Container(
height: MediaQuery.of(context).size.height,
child: _listSearch.isEmpty
? Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
color: Colors.white,
// padding: EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
child: ListView.builder(
padding: EdgeInsets.only(top: 8),
scrollDirection: Axis.vertical,
itemBuilder: (context, _) => Padding(
padding: const EdgeInsets.only(left: 12.0),
child: Row(
children: <Widget>[
Container(
margin: EdgeInsets.only(bottom: 12),
height: 110,
width: 110,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(8),
color: Colors.white),
),
Column(
crossAxisAlignment:
CrossAxisAlignment.start,
mainAxisAlignment:
MainAxisAlignment.start,
children: <Widget>[
Container(
margin: EdgeInsets.only(
bottom: 8, top: 8, left: 8),
height: 10,
width: 150,
color: Colors.white,
),
Container(
margin: EdgeInsets.only(
bottom: 8, left: 8),
height: 10,
width: 200,
color: Colors.white,
),
Container(
margin: EdgeInsets.only(left: 8),
height: 10,
width: 80,
color: Colors.white,
),
SizedBox(
height: 3,
),
],
),
],
),
),
itemCount: 6,
),
),
],
))
: SmartRefresher(
enablePullDown: true,
enablePullUp: false,
header: WaterDropMaterialHeader(),
controller: _refreshController,
onRefresh: _onRefresh,
onLoading: _onLoading,
child: Container(
padding: EdgeInsets.only(bottom: 70),
height: 400,
child: _listSearch.isEmpty
? Center(child: CircularProgressIndicator())
: ListView.builder(
physics: BouncingScrollPhysics(),
shrinkWrap: true,
itemCount: _listSearch.length,
itemBuilder: (context, index) {
if (qtyList[index] != 0) {
postDetail[index] = {
"id_sampah": _listSampah[index].id,
"Jumlah": qtyList[index],
"harga": _listSampah[index].hargaSetor
};
namaSampah[index] =
_listSampah[index].nama;
hargaSetor[index] =
_listSampah[index].hargaSetor;
} else {
postDetail[index] = 0;
namaSampah[index] = 0;
hargaSetor[index] = 0;
}
Result sampah = _listSampah[index];
return Container();
},
style: TextButton.styleFrom(
backgroundColor:
Color(
0xffE0E0E0),
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(10))),
),
)
: Container(
margin: EdgeInsets
.all(10),
width: 35,
height: 35,
child: TextButton(
onPressed:
() {},
child: Icon(
Icons.remove,
size: 20,
color: Color(
0xff909090),
),
style: TextButton.styleFrom(
backgroundColor:
Color(
0xffE0E0E0),
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(10))),
),
),
Text(qtyList[index]
.toString()),
Container(
margin:
EdgeInsets.all(
10),
width: 35,
height: 35,
child: TextButton(
child: Align(
alignment:
Alignment
.center,
child: Icon(
Icons.add,
size: 20,
color: Color(
0xff909090),
)),
onPressed: () {
setState(() {
qtyList[index] =
qtyList[index] +
1;
totalHarga[
index] = _listSampah[
index]
.hargaSetor! *
qtyList[
index];
if (totalHarga.reduce(
(a, b) =>
a +
b) ==
0) {
setor = Colors
.grey;
} else {
setor = Colors
.green;
}
});
},
style: TextButton.styleFrom(
backgroundColor:
Color(
0xffE0E0E0),
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(
10))),
)),
Text(
"Rp : " +
sampah.hargaSetor!
.toString(),
style: TextStyle(
fontWeight:
FontWeight
.bold),
)
],
),
)
],
),
)
],
),
);
}),
),
)),
]),
),
),
DraggableScrollableSheet(),
Align(
alignment: Alignment.bottomCenter,
child: Container(
width: MediaQuery.of(context).size.width,
height: 100,
color: Colors.white,
child: ListView.builder(
itemCount: _listSampah.length,
itemBuilder: (context, index) {
return Column(
children: [
Container(
color: Colors.white,
margin: EdgeInsets.all(8),
padding: const EdgeInsets.all(5.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Total"),
Text(
"Rp. ${totalHarga.reduce((a, b) => a + b)}",
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.bold),
)
],
),
),
Container(
width: MediaQuery.of(context).size.width,
height: 50,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Color(0xff85d057),
onPrimary: Colors.white, // foreground
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Sukses()));
},
child: Text(
'Jual Sampah',
style: TextStyle(
fontSize: 19,
fontWeight: FontWeight.bold,
fontFamily: "Roboto"),
)),
),
],
);
})),
),
])));
}
}
这是我的屏幕模型
// To parse this JSON data, do
//
// final getSampah = getSampahFromMap(jsonString);
import 'dart:convert';
class GetSampah {
GetSampah({
this.hasil,
this.result,
});
bool? hasil;
List<Result>? result;
factory GetSampah.fromJson(String str) => GetSampah.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory GetSampah.fromMap(Map<String, dynamic> json) => GetSampah(
hasil: json["hasil"],
result: List<Result>.from(json["result"].map((x) => Result.fromMap(x))),
);
Map<String, dynamic> toMap() => {
"hasil": hasil,
"result": List<dynamic>.from(result!.map((x) => x.toMap())),
};
}
class Result {
Result({
this.id,
this.nama,
this.kode,
this.jenis,
this.jumlah,
this.hargaSetor,
this.hargaJual,
this.foto,
this.createdAt,
this.v,
});
String? id;
String? nama;
String? kode;
String? jenis;
String? jumlah;
int? hargaSetor;
int? hargaJual;
String? foto;
DateTime? createdAt;
int? v;
factory Result.fromJson(String str) => Result.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Result.fromMap(Map<String, dynamic> json) => Result(
id: json["_id"],
nama: json["Nama"],
kode: json["Kode"],
jenis: json["Jenis"],
jumlah: json["Jumlah"],
hargaSetor: json["HargaSetor"],
hargaJual: json["HargaJual"],
foto: json["Foto"],
createdAt: DateTime.parse(json["CreatedAt"]),
v: json["__v"],
);
Map<String, dynamic> toMap() => {
"_id": id,
"Nama": nama,
"Kode": kode,
"Jenis": jenis,
"Jumlah": jumlah,
"HargaSetor": hargaSetor,
"HargaJual": hargaJual,
"Foto": foto,
"CreatedAt": createdAt!.toIso8601String(),
"__v": v,
};
}
【问题讨论】: