'How Can I Clone a Map List to Another Map List?
My map list on another list and I use switch case in my main page I need to transfer my list to a new list but I can't. As you can see there is a 'lastproduct' and it is a string I made a questioning and tried to make transfer a map list to another new one. It doesn't work. Is there a any mistake?
import 'package:floria/scripts/gridviewcustom.dart';
import 'package:floria/scripts/productlist.dart';
import 'package:flutter/material.dart';
class PurchasingPage extends StatefulWidget {
PurchasingPage(this.lastproduct, {Key? key}) : super(key: key);
final String lastproduct;
List<Map<String, String>> lastname = [];
@override
identifyproductname(lastproduct) {
switch (lastproduct) {
case 'Zambak':
return lastname.addAll(gerbera);
case 'Gül':
return lastname.addAll(rose);
case 'Papatya':
return lastname.addAll(daisy);
case 'Şakayık':
return lastname.addAll(peony);
}
}
void initState() {
identifyproductname(lastproduct);
}
State<PurchasingPage> createState() => _PurchasingPageState();
}
class _PurchasingPageState extends State<PurchasingPage> {
int optionSelected = 0;
void checkOption(int index) {
setState(() {
optionSelected = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
flexibleSpace: Container(
decoration:
BoxDecoration(gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [Theme.of(context).primaryColor, Theme.of(context).secondaryHeaderColor])),
),
title: Container(
width: 90,
height: 45,
child: Image.asset(
"assets/images/floria.png",
fit: BoxFit.fill,
)),
centerTitle: true,
),
body: Center(
child: GridView.count(
crossAxisCount: 2,
mainAxisSpacing: 5,
crossAxisSpacing: 5,
children: [
for (var i = 0; i < widget.lastname.length ; i++)
GridViewCustom(
productname: widget.lastname[i]['Productname'] as String,
img: widget.lastname[i]['img'] as String,
onTap: () => checkOption(i + 1),
selected: i + 1 == optionSelected,
)
],
),
),
);
}
}
As you can see there is a 'lastproduct' and it is string I wanna make a questioning and
Solution 1:[1]
Not really understand what you try to add with rose, daisy..
But here I have created an extension that always returns a new list, work event with immutable lists.
import 'package:flutter/foundation.dart';
void main() {
const a = <Map<String, Object>>[
{'a': 'A'},
{'b': 'B'},
{'c': 'C'},
];
const b = <Map<String, Object>>[
{'b': 'B'},
];
print(a + b); // [{a: A}, {b: B}, {c: C}, {b: B}]
print(a - b); // [{a: A}, {c: C}]
}
extension on Iterable<Map<String, Object>> {
Iterable<Map<String, Object>> operator +(List<Map<String, Object>> other) {
return followedBy(other);
}
Iterable<Map<String, Object>> operator -(List<Map<String, Object>> other) {
return where((a) => !other.any((b) => mapEquals(a, b))).toList();
}
}
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 | Arnas |
