When I try to parse JSON response received from restful API I get an error as Unhandled Exception: NoSuchMethodError: Class 'Response' has no instance method '[].
AuthService().addStaff(new_name, phno, uid, pasword).then((val) {
print(val)
print(val["result"]);
// if (val['result'] == "successfull") {}
addStaff calls an API and get a JSON response , when I print val value I get JSON response as this `
{"Name":"jj","password":"38b","phoneno":"77","result":"successfull","uploadid":"b","user_type":"staff"}
but when I try to parse each individual element say val['result'], I get an exception error saying Class 'Response' has no instance method '[] How can I parse each individual value from response like Name, password, phoneno, result, uploaded, user_type? I'm new to flutter dart not much familiar with REST API handling, can anyone help me to achieve this??
Edit: You need to parse JSON use jsonDecode()
import 'dart:convert';
...
var data = jsonDecode(val);
print(data["result"]);
...
OR
You may have not created the object properly.
check for this type of declaration:
Foo obj; // wronge way of creating object
obj.method();
and change to this
Foo obj = Foo(); // Corret way of creating object
obj.method();
or
Foo obj = new Foo(); // Corret way of creating object
obj.method();
PS: the new keyword is now optional in Dart.
I hope, this is the solution you are looking for.