Shallow & Deep Copy a Map In Dart
Dart supports Map information type. You can copy a Map into another one utilizing different strategies. There are two sorts of copy, shallow copy, and Deep copy. Understanding the distinction between those two is important before you pick how to copy a map.
This article will explore the Shallow & Deep Copy a Map In Dart. We will execute a demo program and I might want to tell you how to copy a Map in Dart and explain the difference between shallow copy and deep copy in your applications.
If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.
Table Of Contents::
Shallow Copy:
A shallow copy means the main object which is the Map is copied, however, the internal objects (the components) are not. An object can be mutable or changeless.
If the component is immutable and you change the value, Dart can not adjust it and another object with an alternate memory area will be made.
Subsequently, utilizing shallow copy ought to be protected if the component sort of the Map is immutable, like String, bool, int, or bool. The following are instances of shallow copy utilizing Dart’s strategies.
Using Map.of:
The Map.of
factory technique can be utilized to copy a Map into a another one.
factory Map.of(Map<K, V> other)
Demo:
var values = <int, String>{
1: 'red',
2: 'green',
3: 'blue',
};
var newValues = Map.of(values);
Using Map.from:
This technique is likeMap.of
, however, you can utilize it to produce another Map whose key as well as component type is a subtype of the source key or component type.
factory Map.from(Map other)
Making another Map with additional exact types is utilized. You need to ensure that all the keys and values have the more exact types.
Demo:
var values = <num, String>{
1: 'red',
2: 'green',
3: 'blue',
};
Map<int, String> newValues = Map.from(values);
Using Map.unmodifiable:
If you want to copy into a Map that cannot be modified, you can use Map.umodifiable
factory method.
factory Map.unmodifiable(Map<dynamic, dynamic> other)
Demo:
var types = Map.unmodifiable({
1: 'red',
2: 'green',
3: 'blue',
});
Deep Copy:
While shallow copy works for a Map with immutable components, you might have to involve a deep copy for a Map with mutable components. Utilizing the models above, assuming that the component is mutable, Dart will just copy the reference to the object.
The actual object isn’t copied. Consequently, assuming that you alter a component of the source, it additionally influences the element of the new Map. On the off chance that you don’t need that way of behaving, you need to clone every component by making another object.
For demo, we have a Map whose component type is Item. To make another Item object from a current one, we can make a factory technique clone.
class Item {
String name;
Item({
required this.name,
});
factory Item.clone(Item source) {
return Item(
name: source.name,
);
}
}
From that point forward, use map the technique to map every component. You want to return a MapEntry and utilize the cloning strategy above to copy the mutable object.
var item1 = Item(name: 'red');
var item2 = Item(name: 'green');
Map<int, Item> source = {1: item1, 2: item2};
var clone = source.map((key, value) => MapEntry(key, Item.clone(value)));
item1.name = 'green';
print(source[1]?.name); // Green
print(clone[1]?.name); // Red
Conclusion:
In the article, I have explained the Shallow & Deep Copy a Map In Dart; you can modify this code according to your choice. This was a small introduction to the Shallow & Deep Copy a Map In Dart User Interaction from my side, and it’s working using Flutter.
That is how to copy a Map in Dart. If the Map just holds back immutable key and component values, shallow values ought to be sufficient. If either the key or value has mutable components, you might need to perform a deep copy.
I hope this blog will provide you with sufficient information on Trying the Shallow & Deep Copy a Map In Dart of your projects. So please try it.
❤ ❤ Thanks for reading this article ❤❤
If I got something wrong? Let me know in the comments. I would love to improve.
Clap 👏 If this article helps you.
Feel free to connect with us:
And read more articles from FlutterDevs.com.
FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on Facebook, GitHub, Twitter, and LinkedIn.
We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.