(by @andrestaltz)
If you prefer to watch video tutorials with live-coding, then check out this series I recorded with the same contents as in this article: Egghead.io - Introduction to Reactive Programming.
(by @andrestaltz)
If you prefer to watch video tutorials with live-coding, then check out this series I recorded with the same contents as in this article: Egghead.io - Introduction to Reactive Programming.
import 'dart:async'; | |
import 'package:http/http.dart' as http; | |
final response = await http.get(myEndpointUrl); | |
if (response.statusCode == 200) { | |
// use the data in response.body | |
} else { | |
// handle a failed request | |
} |
final myObject = serializers.deserializeWith( | |
SimpleObject.serializer, json.decode(aJsonString)); |
final myObject = SimpleObject.fromJson(json.decode(aJsonString)); |
import 'dart:convert'; | |
try { | |
final parsed = json.decode(aJsonStr); | |
} on FormatException catch (e) { | |
print("That string didn't look like Json."); | |
} on NoSuchMethodError catch (e) { | |
print('That string was null!'); | |
} |
final dynamicListOfInts = json.decode(aJsonArrayOfInts); | |
// Create a strongly typed list with references to the data that are casted | |
// immediately. This is probably the better approach for data model classes. | |
final strongListOfInts = List<int>.from(dynamicListOfInts); | |
// Or create a strongly typed list with references that will be lazy-casted | |
// when used. | |
final anotherStrongListOfInts = List<int>.from(dynamicListOfInts); |
{ | |
"aString": "Blah, blah, blah.", | |
"anInt": 1, | |
"aDouble": 1.0, | |
"aListOfStrings": ["one", "two", "three"], | |
"aListOfInts": [1, 2, 3], | |
"aListOfDoubles": [1.0, 2.0, 3.0] | |
} |
class SimpleObject { | |
const SimpleObject({ | |
this.aString, | |
this.anInt, | |
this.aDouble, | |
this.aListOfStrings, | |
this.aListOfInts, | |
this.aListOfDoubles, | |
}); |
return SimpleObject( | |
aString: json['aString'] ?? "", | |
anInt: json['anInt'] ?? 0, | |
aDouble: json['aDouble'] ?? 1.0, | |
aListOfStrings: List.from(json['aListOfStrings'] ?? []), | |
aListOfInts: List.from(json['aListOfInts'] ?? []), | |
aListOfDoubles: List.from(json['aListOfDoubles'] ?? []), | |
); |
class SimpleObject { | |
SimpleObject({ | |
this.aString, | |
this.anInt, | |
this.aDouble, | |
this.aListOfStrings, | |
this.aListOfInts, | |
this.aListOfDoubles, | |
}); |