What is the difference and preferred way between jsonEncode() and json.encode() in Dart?

Issue

In Flutter doc, it uses jsonEncode(), while in Angular doc, it uses json.encode(). What is the difference and preferred way between the two?

Solution

jsonEncode as alias for json was introduced because json often collided with a varible name json many used for the variable that holds the JSON value.

var json = http.get(...);
var data = json.decode(json); // error
var data = jsonDecode(json); // ok

Answered By – Günter Zöchbauer

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *