Alexandru Buruiana
Alexandru Buruiana

Reputation: 137

Flutter generate JSON from widget

I want to make my app dynamic. Is there a way I can generate JSON from my widgets in any flutter application?

I found solutions just for parsing from JSON to widget, but I need the other way around.

Upvotes: 0

Views: 379

Answers (1)

Sajjad
Sajjad

Reputation: 3228

honestly I am not sure this is best way or not ... but I think you can use something like below to generate JSON manually ...

myPrint(dynamic widget){
  if(widget is Column || widget is Row || widget is Stack){
    for(dynamic w in widget.children){
      print(widget.toString());
      myPrint(w);
    }
  }else{
    print(widget.toString());
    try {
      myPrint(widget.child);
    } catch (e) {
    }
  }
}

for Example below Widget ...

var myWidget = Column(
  children: [
    Center(
      child: Text("word" , style: TextStyle(fontSize: 25),),
    ),
  ],
);   

prints like this...

Column(direction: vertical, mainAxisAlignment: start, crossAxisAlignment: center)

Center(alignment: center)

Text("word", inherit: true, size: 25.0)

Upvotes: 1

Related Questions