Last active
June 8, 2020 14:36
-
-
Save branflake2267/31aa8a6ce9b2f4f3956c433e3acfb60d to your computer and use it in GitHub Desktop.
Flutter - Upgrading to Dart 2, JSON & Casting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!
I've got another example in a recent project here, with a youtube video too.
https://github.com/branflake2267/debugging-flutter-android-ios-web-dartwebserver/blob/master/client/lib/MyApp.dart#L50 - source
https://github.com/branflake2267/debugging-flutter-android-ios-web-dartwebserver - project