Reputation: 165
This is my first time posting here, so I apologize if I did this incorrectly.
I made a custom class with a boolean field to control the CheckBox state/value:
class CustomModel {
static const TITLE_KEY = 'title';
String id;
String title;
bool isChecked = false;
void setIsChecked(bool isChecked) {
this.isChecked = isChecked;
}
bool getIsChecked() {
return isChecked;
}
CustomModel(Map<String, dynamic> ex) {
title = ex[TITLE_KEY];
}
}
In my CheckBox widget:
return new ExpansionTile(
title: const Text('Title'),
backgroundColor: Colors.grey[100],
children: snapshot.data.documents
.map((DocumentSnapshot document) {
CustomModel ex = new CustomModel(document.data);
return new ListTile(
title: new Text(ex.title),
trailing: new Checkbox(
value: ex.getIsChecked(),
onChanged: (bool value) {
setState(() {
ex.setIsChecked(value);
});
}),
);
}).toList());
}),
The CheckBox doesn't change (kind of flickers) when clicked. The logs show the CheckBox is "false" and then "true", but if I click on the same CheckBox again, the logs show "false" and then "true", so it doesn't seem to be updating the field.
I didn't have to set a value to "isChecked" when I did this in Java, but Flutter is throwing an error if I don't.
I/flutter ( 6408): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 6408): The following assertion was thrown building StreamBuilder(dirty, state: I/flutter ( 6408): _StreamBuilderBaseState>#0512c): I/flutter ( 6408): 'package:flutter/src/material/checkbox.dart': Failed assertion: line 63: 'tristate || value != I/flutter ( 6408): null': is not true.
Thank you in advance!
Upvotes: 0
Views: 2135
Reputation: 2558
I assume that return new ExpansionTile(..);
is in your the build()
method of the StatefulWidget
class. So when you call setState()
, it will re-build widgets, which mean calling the return new ExpansionTile(..);
again. Since your CustomModel ex = new CustomModel(document.data);
will also be instantiate again, and isChecked
will be false at that point. So the solution is to put all your build CustomModel
list from firestore in initState().
This is the idea, (there may be some syntax errors because I write it straight on here).
class MyWidget extends StatefulWidget {
State createState() => new MyWidgetState(); }
class MyWidgetState extends State<MyWidget> {
List<CustomModel> customModels = [];
@override
initState() {
//use listener to fetch data
Firestore.instance.collection("name").snapshots.listen((snapshot) {
for (var doc in snapshot.documents) {
CustomModel ex = new CustomModel(fieldname: doc['fieldname]);
customModels.add(ex);
}
}
}
@override
Widget build(BuildContext) {
return new ExpansionTile(
title: const Text('Title'),
backgroundColor: Colors.grey[100],
children: customModels.map((CustomModel ex) {
return new ListTile(
title: new Text(ex.title),
trailing: new Checkbox(
value: ex.getIsChecked(),
onChanged: (bool value) {
setState(() {
ex.setIsChecked(value);
});
}),
);
}).toList());
}),
}
}
Upvotes: 3