dartflutterangular-dart

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


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