I am new to flutter and developing an application in which I am displaying data in the from of ListView. There is a delete IconButton in it. I want that the selected ListTile data should be deleted. It is displaying data in the form of List but not deleting. After I have added the deleteData() method. It is showing the following error:
'package:flutter/src/rendering/silver_multi_box_adaptor.dart':Failed assertion: line 263 pos 16: 'child == null || indexOf(child) > index' : is not true.
Any help is highly appreciated. Here is what I have done so far:
class OrderRequestModel extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: UserOrderRequestModel(),
theme: new ThemeData(
primarySwatch: Colors.red,
),
);
}
}
class UserOrderRequestModel extends StatefulWidget {
@override
_UserOrderRequestModelState createState() {
return _UserOrderRequestModelState();
}
}
class _UserOrderRequestModelState extends State<UserOrderRequestModel> {
deleteData(docId) {
Firestore.instance
.collection('order_tbl')
.document(docId)
.delete()
.catchError((e) {
print(e);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _buildBody(context),
);
}
Stream<QuerySnapshot> getData() async*{
FirebaseUser firebaseUser = await FirebaseAuth.instance.currentUser();
yield* Firestore.instance.collection('order_tbl').orderBy('timestamp', descending: true).snapshots();
}
Widget _buildBody(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: getData(),
builder: (context, snapshot) {
if (!snapshot.hasData) return LinearProgressIndicator();
return _buildList(context, snapshot.data.documents);
},
);
}
Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) {
return ListView(
padding: const EdgeInsets.only(top: 5.0),
children: snapshot.map((data) => _buildListItem(context, data)).toList(),
);
}
Widget _buildListItem(BuildContext context, DocumentSnapshot data) {
final record = Record.fromSnapshot(data);
String _string =getTimeDifferenceFromNow(record.timestamp.toDate());
return Padding(
key: ValueKey(record.animal),
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(5.0),
),
child: new ListTile(
title: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Text(
"${record.quantity} kg ${record.animal} - ${record.mobile}",
style: new TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
),
new Text(
_string,
style: new TextStyle(color: Colors.grey, fontSize: 12.0),
),
],
),
trailing: IconButton(
icon: Icon(Icons.delete_forever),
onPressed: () => deleteData(data.documentID),
),
subtitle: new Container(
padding: const EdgeInsets.only(top: 5.0),
child: new Text(
record.address,
style: new TextStyle(color: Colors.red, fontSize: 12.0,),
),
),
),
),
);
}
}