Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Last active June 8, 2020 14:36
Show Gist options
  • Save branflake2267/31aa8a6ce9b2f4f3956c433e3acfb60d to your computer and use it in GitHub Desktop.
Save branflake2267/31aa8a6ce9b2f4f3956c433e3acfb60d to your computer and use it in GitHub Desktop.
Flutter - Upgrading to Dart 2, JSON & Casting
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var cacheddata = new Map<int, Data>();
var offsetLoaded = new Map<int, bool>();
int _total = 0;
@override
void initState() {
_getTotal().then((int total) {
setState(() {
_total = total;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
var listView = new ListView.builder(
itemCount: _total,
itemBuilder: (BuildContext context, int index) {
Data data = _getData(index);
return new ListTile(
title: new Text(data.value),
);
}
);
return new Scaffold(
appBar: new AppBar(
title: new Text("App Bar Title"),
),
body: listView,
);
}
Future<List<Data>> _getDatas(int offset, int limit) async {
String jsonString = await _getJson(offset, limit);
List list = json.decode(jsonString) as List;
var datas = new List<Data>();
list.forEach((element) {
Map map = element as Map;
datas.add(new Data.fromMap(map));
});
return datas;
}
Future<String> _getJson(int offset, int limit) async {
String json = "[";
for (int i= offset; i < offset + limit; i++) {
String id = i.toString();
String value = "value ($id)";
json += '{ "id":"$id", "value":"$value" }';
if (i < offset + limit - 1) {
json += ",";
}
}
json += "]";
await new Future.delayed(new Duration(seconds: 3));
return json;
}
Data _getData(int index) {
Data data = cacheddata[index];
if (data == null) {
int offset = index ~/ 5 * 5;
if (!offsetLoaded.containsKey(offset)) {
offsetLoaded.putIfAbsent(offset, () => true);
_getDatas(offset, 5)
.then((List<Data> datas) => _updateDatas(offset, datas));
}
data = new Data.loading();
}
return data;
}
Future<int> _getTotal() async {
return 1000;
}
void _updateDatas(int offset, List<Data> datas) {
setState((){
for (int i=0; i < datas.length; i++) {
cacheddata.putIfAbsent(offset + i, () => datas[i]);
}
});
}
}
class Data {
String id;
String value;
Data.loading() {
value = "Loading...";
}
Data.fromMap(Map map) {
id = map['id'];
value = map['value'];
}
}
@branflake2267
Copy link
Author

branflake2267 commented Apr 1, 2018

The previous episode's source can be found here:
https://gist.github.com/branflake2267/dd7251ace7997622d585b4d0769c28d3

@branflake2267
Copy link
Author

branflake2267 commented Apr 2, 2018

The more elegant solution for casting could be if you know the type:

/// [json] is the library variable which is a compiled constant of JsonCodec().
List<Map> list = json.decode(jsonString).cast<Map>();

@carnegiejunior
Copy link

Dude... you're rock!! all that I was looking for... pretty good!! Congratulations...
Brazilian portuguese: "Tudo que eu estava procurando.. valeu!"

@Allanbikundo
Copy link

THANKS !!!

@attibalazs
Copy link

i spent a lot of time looking for something like this, thank you!

@branflake2267
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment