Dart (Flutter) Clone Object

Haşim Yerlikaya
Kodiks
Published in
1 min readMar 20, 2020

Hello everyone,

I needed to clone an object in the project I was working on. I did a little research on this. Even though there are dart packages developed for this job, I would like to talk about an easier and more effective method.

I think most of us are already doing json serilization in their projects. We have the fromJson and toJson methods. We can use these methods for the object we want to clone.

I go to the codes without extending much :)

I added a method called clone, in this method I first convert my object to json string and then convert it back to the object. Thus, I create a new object with the data. (You must add “import ‘dart:convert’;” package)

I hope it will be useful, thanks for reading.

--

--