Created
March 26, 2019 20:20
-
-
Save brucevang/9c1af49b16f72beb8b5a65df62005fec to your computer and use it in GitHub Desktop.
ListViewJSONLocal.dart
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 'package:flutter/material.dart'; | |
// Needed for JSON from URL | |
import 'dart:async'; | |
import 'package:http/http.dart' as http; | |
// Needed to Conver JSON Network and Local file | |
import 'dart:convert'; | |
// Needed for JSON from local | |
// UPDATE: No, Not Needed | |
import 'package:flutter/services.dart' show rootBundle; | |
import 'package:json_annotation/json_annotation.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
// Call JSON from Local File | |
Future<List<Movies>> _getMovies() async { | |
var data = await DefaultAssetBundle.of(context).loadString("lib/assets/movies.json"); | |
var jsonResult = json.decode(data); | |
List<Movies> users = []; | |
for (var u in jsonResult) { | |
Movies user = | |
// This order must match the order in the JSON definition at the bottom | |
Movies(u["index"], u["picture"], u["name"]); | |
users.add(user); | |
} | |
print(users.length); | |
return users; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Container( | |
child: FutureBuilder( | |
future: _getMovies(), | |
builder: (BuildContext context, AsyncSnapshot snapshot) { | |
if (snapshot.data == null) { | |
return Container( | |
child: Center( | |
child: Text('loading...'), | |
), | |
); | |
} else { | |
return ListView.builder( | |
itemCount: snapshot.data.length, | |
itemBuilder: (BuildContext context, int index) { | |
return ListTile( | |
leading: CircleAvatar( | |
backgroundImage: | |
NetworkImage(snapshot.data[index].picture), | |
), | |
title: Text(snapshot.data[index].name), | |
//subtitle: Text(snapshot.data[index].email), | |
onTap: (){ | |
// Navigator.push(context, | |
// new MaterialPageRoute(builder: (context) => ListDetailView(snapshot.data[index])) | |
// ); | |
}, | |
); | |
}, | |
); | |
} | |
}, | |
), | |
), | |
); | |
} | |
} | |
class Movies { | |
final int index; | |
final String name; | |
final String picture; | |
Movies(this.index, this.picture, this.name); | |
} | |
class Student{ | |
String studentId; | |
String studentName; | |
int studentScores; | |
Student({ | |
this.studentId, | |
this.studentName, | |
this.studentScores | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment