'adding values to an empty map in dart
please does anyone know how I can add the value coming from a populated map to an already initialized empty map? type 1: This is just an example I am running on a flutter application to try it because of the hot reload feature. It's supposed to return the map in the map as a logged result. it returns an empty list since it is the value of the already initialized map. what I want is for it to return the added value from the map to the empty map and log.
class _ShowAppState extends State<ShowApp> {
final usermap = {
'docreference1': {
'college': 'Engineering',
'name': 'ola',
},
'docreference2': {
'college': 'Engineering',
'name': 'ola b',
},
'docreference3': {
'college': 'Engineering',
'name': 'ola a',
}
};
@override
Widget build(BuildContext context) {
final Map<String, String> stud = {};
// usermap.forEach((key, value) {
// final stud = value;
// stud;
// });
usermap.values.map((values) {
stud.addAll(values);
});
log('=====> $stud');
type 2: since my firebase request returns a map of map since I am querying a list of an object I need to pass the value of the map from Map<String,Map<String, dynamic>> to Map<String, dynamic>. and I can't return the value of .foreach or .map because their return type is null. so I thought to populate an already initialized empty list how can I archive this. without it returning null.
Future<Either<DtFailure, KtList<Student>>> viewAttendance(
String college, String department, String level) async {
try {
final snapshot = await _firebaseDatabase
.ref()
.child(
"studentsDetail/${_firebaseAuth.currentUser?.uid}/$college/$department/$level")
.orderByKey()
.get();
if (snapshot.exists) {
final KtList<Student> students = KtList.of();
// final students = DocRef.fromJson(data as Map<String, dynamic>);
final data = snapshot.value as Map<String, dynamic>;
data.forEach((key, value) {
final studentdata = value as Map<String, dynamic>;
final students = (studentdata)
.map((key, value) => MapEntry(
key, Student.fromJson(value as Map<String, dynamic>)))
.values
.toImmutableList();
students;
});
return right(students);
} else {
return right(const KtList.empty());
}
} on FirebaseException catch (e) {
if (e.message!.contains('PERMISSION_DENIED')) {
return left(const DtFailure.insufficientPermission());
} else {
return left(const DtFailure.unexpected());
}
}
}
Solution 1:[1]
In type 1, what is your expected output? If you want all the value from your key-value pair and store that in a variable then you can do something like this:
final usermap = {
'docreference1': {
'college': 'Engineering',
'name': 'ola',
},
'docreference2': {
'college': 'Engineering',
'name': 'ola b',
},
'docreference3': {
'college': 'Engineering',
'name': 'ola a',
}
};
final stud = (userMap.values).toList();
print(stud); // [{college: Engineering, name: ola}, {college: Engineering, name: ola b}, {college: Engineering, name: ola a}]
stud.forEach(print); /* {college: Engineering, name: ola}
{college: Engineering, name: ola b}
{college: Engineering, name: ola a} */
Now that you have a List<Map<String, String>> in variable stud, you can easily use the list's index to get the proper Map item.
If you were expecting for an output that looks something like this:
{
college: Engineering, name: ola,
college: Engineering, name: ola b,
college: Engineering, name: ola a
}
This is not possible as you can't have a Map with 2 or more keys being equal. Keys must be unique.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
