Flutter - Load JSON Assets
Last Updated :
28 Apr, 2025
In Flutter, you can load JSON assets using the rootBundle method from the flutter/services package. First, create a new Flutter project by running the following command in your terminal.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Add the material package that gives us the important methods and then call the runApp method in the main function that will call our application.
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
Step 3: Now we have to make a stateful widget RunMyApp Because our application does go to change its state and then return the materialApp widget which allows us the set the title and theme and many more.
Step 4: Next, create a new file named data.json in the assets directory of your project. The assets directory should be located at the same level as the lib directory. In the data.json file, add some JSON data. For example:
{
"name": "John Doe",
"age": 30,
"email": "[email protected]"
}
Step 5: Open the pubspec.yaml file and add the following code to specify the asset.
Dart
flutter:
assets:
- assets/data.json
Step 6: In your Dart code, import the flutter/services package:
Dart
import 'package:flutter/services.dart' show rootBundle;
To load the JSON data from the asset, use the rootBundle.loadString method as shown below:
Dart
Future<void> loadJsonAsset() async {
final String jsonString = await rootBundle.loadString('assets/data.json');
final data = jsonDecode(jsonString);
print(data);
}
The loadString method returns a Future<String> that represents the contents of the file. The jsonDecode method from the dart:convert library is then used to convert the JSON string to a Dart object. Call the loadJsonAsset method to load the JSON data when your app starts:
Dart
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var jsonData;
Future<void> loadJsonAsset() async {
final String jsonString = await rootBundle.loadString('assets/data.json');
var data = jsonDecode(jsonString);
setState(() {
jsonData = data;
});
//print(jsonData);
}
@override
void initState() {
// TODO: implement initState
super.initState();
loadJsonAsset();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
home: Scaffold(
appBar: AppBar(
title: Text('Load Assets Json'),
),
body: Center(
child: jsonData != null
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [Text(jsonData['name']), Text(jsonData['email'])],
)
: CircularProgressIndicator()),
),
);
}
}
Output :
Similar Reads
Flutter - Load Text Assets In Flutter, you can easily load text assets such as JSON files, configuration files, or plain text files into your app's memory at runtime. This allows you to access the data contained in these files and use it to populate your app's UI or perform other operations. To load text assets in Flutter, yo
3 min read
Flutter - Asset Image Flutter is an open-source, cross-platform UI development kit developed by Google. It is gaining popularity these days, as the app made in Flutter can run on various devices regardless of their platform. It is majorly used to develop applications for Android and iOS, as a single app made in Flutter c
2 min read
Flutter - Read JSON Data from Assets Folder A JSON object contains data in the form of key/value pair. The keys are strings and the values are the JSON types. Keys and values are separated by a colon. Each entry (key/value pair) is separated by a comma. We will learn how to read the data already stored in your project folder in your app with
3 min read
Flutter - Load Your Image Assets Faster In Flutter, images play a crucial role in creating visually appealing and engaging user interfaces. However, loading images from the network or assets can sometimes lead to slow performance and hinder the user experience. To tackle this issue, Flutter provides a convenient method called precacheImag
4 min read
Flutter - Carousel Slider Carousel Slider is one of the most popular image sliders used nowadays in most apps. These carousel sliders are mostly seen on various eCommerce sites such as Amazon, Flipkart, Myntra, and many more. Displaying the images in a slider gives an attractive User Experience. As these sliders are automate
4 min read
Flutter - Convert JSON to Model Class in Dart Many times you are receiving API data but it is difficult to create a model class manually for some data that are too big. So we can make a model with just 2 steps. Let's take sample data for which we want to create a model class. { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": f
2 min read