1
0
mirror of https://github.com/nisrulz/flutter-examples.git synced 2026-08-25 09:13:00 +00:00
Files
flutter-examples/load_local_json/lib/example.dart
Nishant Srivastava 780c0f1dbb refactor: split app wrapper from focused example code (single-file apps)
Convert 20 more single-file apps to the validated pattern: lib/main.dart
holds only the runApp + MaterialApp bootstrap, while a new lib/example.dart
holds the full screen (Scaffold, AppBar) and the focused example code.

Apps: enabling_splash_screen, getx_counter_app, image_from_network,
infinite_list, load_local_image, load_local_json, persist_key_value,
push_notifications, sliver_app_bar_example, stateless_widgets, tic_tac_toe,
tip_calculator, using_alert_dialog, using_edittext, using_expansionpanel,
using_http_get, using_interactiveviewer, using_snackbar, using_stepper,
using_theme
2026-08-18 00:41:30 +02:00

58 lines
2.1 KiB
Dart

import 'dart:convert';
import 'package:flutter/material.dart';
// Example: load and decode a local JSON asset, then list its rows.
class Example extends StatefulWidget {
const Example({super.key});
@override
State<Example> createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
late List data;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const 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 newData = 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: ${newData[index]['name']}"),
Text("Height: ${newData[index]['height']}"),
Text("Mass: ${newData[index]['mass']}"),
Text("Hair Color: ${newData[index]['hair_color']}"),
Text("Skin Color: ${newData[index]['skin_color']}"),
Text("Eye Color: ${newData[index]['eye_color']}"),
Text("Birth Year: ${newData[index]['birth_year']}"),
Text("Gender: ${newData[index]['gender']}")
],
),
);
},
itemCount: newData == null ? 0 : newData.length,
);
}),
),
));
}
}