Flutter - Read JSON Data from Assets Folder
Last Updated :
24 Apr, 2025
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 just a simple function. Sometimes it happens that we have some that are stored in your project folder/assets folder in your project directory. You want to access that data in your application and have to use it in the same.
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: Create json file in your asset folder and add the data you want to use in your applications
Json Data
// Data you want to get and show or use it
{"data":[
{
"title": "How to Add .env File in Flutter?",
"link": "https://www.geeksforgeeks.org/how-to-add-env-file-in-flutter/"
},
{
"title": "Flutter - Select Single and Multiple Files From Device",
"link": "https://www.geeksforgeeks.org/flutter-select-single-and-multiple-files-from-device/"
},
{
"title": "Autofill Hints Suggestion List in Flutter",
"link": "https://www.geeksforgeeks.org/autofill-hints-suggestion-list-in-flutter/"
},
{
"title": "How to Integrate Razorpay Payment Gateway in Flutter?",
"link": "https://www.geeksforgeeks.org/how-to-integrate-razorpay-payment-gateway-in-flutter/"
},
{
"title": "How to Setup Multiple Flutter Versions on Mac?",
"link": "https://www.geeksforgeeks.org/how-to-setup-multiple-flutter-versions-on-mac/"
},
{
"title": "How to Change Package Name in Flutter?",
"link": "https://www.geeksforgeeks.org/how-to-change-package-name-in-flutter/"
},
{
"title": "Flutter - How to Change App and Launcher Title in Different Platforms",
"link": "https://www.geeksforgeeks.org/flutter-how-to-change-app-and-launcher-title-in-different-platforms/"
},
{
"title": "Custom Label Text in TextFormField in Flutter",
"link": "https://www.geeksforgeeks.org/custom-label-text-in-textformfield-in-flutter/"
}
]
}
Step 3: Add the file path in pubspec.yaml file
assets:
- assets/sample_json.json
# path for the json file
Step 4: Add this code to your 1 file of your choice in the project folder/lib directory
Dart
import 'dart:convert';
import 'package:flutter/services.dart' as root_bundle;
class ReadJsonFile{
static Future<Map> readJsonData({required String path}) async {
// read json file
final jsondata = await root_bundle.rootBundle.loadString(path);
// decode json data as list
final list = json.decode(jsondata) as Map;
// map json and initialize
// using DataModel
return list;
}
}
Step 5: Call this function where you want to access the data
Dart
floatingActionButtonLocation:
FloatingActionButtonLocation.miniCenterFloat,
floatingActionButton: FloatingActionButton.extended(
onPressed: () {
ReadJsonFile.readJsonData(path: "assets/sample_json.json").then((value) {
});
},
label: const Text("Read data from json file"),
),
Step 6: Store this variable and use it where you want
Dart
floatingActionButtonLocation:
FloatingActionButtonLocation.miniCenterFloat,
floatingActionButton: FloatingActionButton.extended(
onPressed: () {
ReadJsonFile.readJsonData(path: "assets/sample_json.json").then((value) {
setState(() {
myList=value["data"];
});
});
},
label: const Text("Read data from json file"),
),
Step 7: We will here show the data that is in the same json file
Dart
Center(
child:
myList.isEmpty?const Text("Please click below button to get the data"): Column(
mainAxisAlignment: MainAxisAlignment.center,
children:List.generate(myList.length, (index) {
return Card(
child: ListTile(
onTap: (){
launchUrl(Uri.parse(myList[index]["link"].toString()));
},
title: Text(myList[index]["title"].toString()),
subtitle:Text(myList[index]["link"].toString()) ,
),
);
}),
),
)
Output:
1. Before pressing the button
Â
2. After pressing the button
Â
Similar Reads
Flutter - Load JSON Assets
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 Andro
3 min read
Flutter - Fetch Data From REST APIs
In this article, we know about how we can fetch the data from the API and show it on our screen dynamically. Dynamically means all the data came from the API using the internet or you can say that you need internet access to fetch the data. For this process, we have to follow some steps to make it s
5 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 - Return Data from Screen
Interaction with UI is an essential part of any application. During the same, there might be a need to return data from the screen. This kind of interaction can range from selecting an option to navigating to different routes through the various buttons in the UI. In this article, we will explore th
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
Flutter - Store Data in Hive Local Database
Hive is a data storage system on our phone where we can store data in boxes. We can store an integer, string, list of strings, Boolean, double, models, integers, etc., in Hive. Now, let us discuss where we can implement these. The first way we can use this is to save the user information user is log
12 min read
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
Retrieve Data From TextFields in Flutter
In this article, we'll learn how to retrieve data from TextFields. TextField() widget is the most common widget used in flutter apps to take user input. We'll talk about two major methods used to extract text from TextField.Using VariablesThe TextField widget has various callback properties through
4 min read
Flutter - Sharing Data Among Flutter Pages
In this article, we are going to find the solution for the problem statement "Import/send data from one page to another in flutter". Before going into the topic, there are some pre-requisites. Pre-requisites:Basics of dart programming language.Setting up Flutter in VS code or Setting up Flutter in A
4 min read
Hosting Flutter Website On Firebase For Free
Flutter is a user interface development software development kit. Flutter is an open-source project maintained by Google. It enables software developers to make beautiful natively compiled applications for IOS, Android, Web, and Desktop with a single code base. Although developing flutter applicatio
4 min read