I want cannot post data from flutter emulator to database. This is the error I got after run the emulator and enter the data.
Exception has occurred. FormatException (FormatException: Unexpected character (at character 1) ^ )
this is my api.dart import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
class CallApi{
final String _url = 'http://10.2.2.0/voyceb/api/';
postData(data, apiUrl) async {
var fullUrl = _url + apiUrl + await _getToken();
return await http.post(
fullUrl,
body: jsonEncode(data),
headers: _setHeaders()
);
}
getData(apiUrl) async {
var fullUrl = _url + apiUrl + await _getToken();
return await http.get(
fullUrl,
headers: _setHeaders()
);
}
_setHeaders() => {
'Content-type' : 'application/json',
'Accept' : 'application/json',
};
_getToken() async {
SharedPreferences localStorage = await
SharedPreferences.getInstance();
var token = localStorage.getString('token');
return '?token=$token';
}
}
the error shown at part in signup.dart
var res = await CallApi().postData(data, 'register');
var body = json.decode(res.body);
error shown at
var body = json.decode(res.body);
at json.decode.
I want data to be insert to database as I enter in emulator.