Convert String to JSON Dart/Flutter

Convert String to JSON Dart/Flutter

In the tutorial, I will introduce how to convert String to JSON Dart/Flutter using “dart:convert” library. With Dart/Flutter, for converting between different data representations (Json or UTF-8), we use “dart:convert library”.

We can use 2 methods for converting String to JSON in Dart/Flutter:

  • json.decode(str)
  • jsonDecode(str)>

Dart jsonDecode function

Parses a Dart string and returns the resulting Json object.


dynamic jsonDecode (
	String source,
	{Object? reviver(
			Object? key,
		Object? value
	)}
)

- The optional reviver function is called once for each object or list property that has been parsed during decoding.
- The key argument is either the integer list index for a list property, the string map key for object properties.

Implementation:

dynamic jsonDecode(String source,
        {Object? reviver(Object? key, Object? value)?}) =>
    json.decode(source, reviver: reviver);

Example 1 in Dart/Flutter - Convert String to JSON using json.decode function

import 'dart:convert';

void main() {
  var greeting = '{"Hello":"world"}';
  Map valueMap = json.decode(greeting);
  print(valueMap);
}

- Output:

{Hello: world}

Example 2 in Dart/Flutter - Convert String to JSON using jsonDecode function


import 'dart:convert';

void main() {
  var str = '{"first_name" : "Jack","last_name" : "Addy","gender" : "male", "location" : { "state" : "NewYork", "country" : "USA"} }';
  Map valueMap = jsonDecode(str);
  print(valueMap);
}

Output:

{first_name: Jack, last_name: Addy, gender: male, location: {state: NewYork, country: USA}}

Flutter Redux Tutorial – A simple practical Flutter Redux example

Flutter Redux Tutorial – A simple practical Flutter Redux example

In this Flutter Redux tutorial, we’re gonna introduce main concept of Redux: what it is, how to work with Redux Store, Action, Reducers. Then we will practice to understand all of them with a simple practical Flutter Redux example.

Continue reading “Flutter Redux Tutorial – A simple practical Flutter Redux example”

Flutter Dismissible example – Swipe to dismiss/remove Item in ListView

In previous post, we had created and shown a ListView with ListView.builder() constructor method. Today, we’re gonna add a function that allows us swipe to remove Item in the list using Dismissible.

Related Post: Flutter ListView example with ListView.builder

Continue reading “Flutter Dismissible example – Swipe to dismiss/remove Item in ListView”

Flutter Firebase Database example – Firebase Database CRUD with ListView

Flutter Firebase Database example – Firebase Database CRUD with ListView

Firebase Realtime Database is a cloud-hosted database that helps us to store and sync data with NoSQL cloud database in realtime to every connected client. In this tutorial, we’re gonna build a Flutter App that allows us to make CRUD interactions with Firebase Database in a ListView.

Related Posts:
How to integrate Firebase into Flutter App – Android Studio
Flutter Navigator example – Send/Return data to/from new Screen
Flutter ListView example with ListView.builder
Flutter Firestore example – Firebase Firestore CRUD with ListView

Continue reading “Flutter Firebase Database example – Firebase Database CRUD with ListView”

Flutter Firestore example – Firebase Firestore CRUD with ListView

Cloud Firestore helps us store data in the cloud. It supports offline mode so our app will work fine (write, read, listen to, and query data) whether device has internet connection or not, it automatically fetches changes from our database to Firebase Server. We can structure data in our ways to improve querying and fetching capabilities. This tutorial shows you a Flutter app that can do Firestore CRUD Operations with ListView widget.

Related Posts:
How to integrate Firebase into Flutter App – Android Studio
Flutter Navigator example – Send/Return data to/from new Screen
Flutter ListView example with ListView.builder

Firebase Database: Flutter Firebase Database example – Firebase Database CRUD with ListView

Continue reading “Flutter Firestore example – Firebase Firestore CRUD with ListView”