Created
October 21, 2019 08:47
-
-
Save k4zek4ge/e2d40955d728b50478e5539267dbc048 to your computer and use it in GitHub Desktop.
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:convert'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MaterialApp( | |
home: MyApp(), | |
)); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
MyAppState createState() => MyAppState(); | |
} | |
class MyAppState extends State<MyApp> { | |
List data; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("Load local JSON file"), | |
), | |
body: Container( | |
child: Center( | |
// Use future builder and DefaultAssetBundle to load the local JSON file | |
child: FutureBuilder( | |
future: DefaultAssetBundle | |
.of(context) | |
.loadString('data_repo/starwars_data.json'), | |
builder: (context, snapshot) { | |
// Decode the JSON | |
var new_data = json.decode(snapshot.data.toString()); | |
return ListView.builder( | |
// Build the ListView | |
itemBuilder: (BuildContext context, int index) { | |
return Card( | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: <Widget>[ | |
Text("Name: " + new_data[index]['name']), | |
Text("Height: " + new_data[index]['height']), | |
Text("Mass: " + new_data[index]['mass']), | |
Text( | |
"Hair Color: " + new_data[index]['hair_color']), | |
Text( | |
"Skin Color: " + new_data[index]['skin_color']), | |
Text( | |
"Eye Color: " + new_data[index]['eye_color']), | |
Text( | |
"Birth Year: " + new_data[index]['birth_year']), | |
Text("Gender: " + new_data[index]['gender']) | |
], | |
), | |
); | |
}, | |
itemCount: new_data == null ? 0 : new_data.length, | |
); | |
}), | |
), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment