convert JSON to array -flutter

Issue

so im using sql_conn dependency on my flutter project to connect database to my flutter project.

im following the example in the flutter package site and using this method

  var res = "";
  Future<void> cetak(String query) async {
    var req = await SqlConn.readData(query);
    setState(() {
      res = req;
    });
  }

the problem is, res is showing an Stringlike this

[{"CUST_NAME" : "MY NAME"}]

and i just want to show MY NAME later in Text() widget.

How do i parse res so i can get only MY NAME Value

Solution

try this, and read more here

import 'dart:convert';
var res = "";
  Future<void> cetak(String query) async {
    var req = await SqlConn.readData(query);
    var parsedJson = jsonDecode(req);
    setState(() {
      res = parsedJson[0]['CUST_NAME'];
    });
  }

Answered By – Dean Van Greunen

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.