Flutter: Unhandled Exception: FormatException: Unexpected character (at character 1) - json

I am new in Flutter. I face a problem about Flutter: Unhandled Exception: FormatException: Unexpected character (at character 1).
This is my Debug Console:
E/flutter ( 8328): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter ( 8328): <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
E/flutter ( 8328): ^
E/flutter ( 8328):
E/flutter ( 8328): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1405:5)
E/flutter ( 8328): #1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1272:9)
E/flutter ( 8328): #2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:937:22)
E/flutter ( 8328): #3 _parseJson (dart:convert-patch/convert_patch.dart:40:10)
E/flutter ( 8328): #4 JsonDecoder.convert (dart:convert/json.dart:612:36)
E/flutter ( 8328): #5 JsonCodec.decode (dart:convert/json.dart:216:41)
E/flutter ( 8328): #6 jsonDecode (dart:convert/json.dart:155:10)
E/flutter ( 8328): #7 _UserCreateEnquiryScreenState.build.<anonymous closure>.<anonymous closure>
package:dragon_lyz_app/…/user/user_create_enquiry_screen.dart:112
E/flutter ( 8328): #8 _rootRunUnary (dart:async/zone.dart:1434:47)
E/flutter ( 8328): #9 _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 8328): <asynchronous suspension>
E/flutter ( 8328):
How do I fix it?

Well, you fix it by not parsing an HTML text as JSON in your build method of the _UserCreateEnquiryScreenState class.
Since you posted no code and no other explanation, that is all the help we can give you.

Related

Provider error while posting to data to server (Using Chopper)

I get the following errors. I am using chopper flutter library and providers. I haven't used providers much. I am sending dummy data but on the form button click.
I/flutter (28363): Observatory listening on ************************************
E/flutter (28363): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Converting object to an encodable object failed: Instance of 'DateTime'
E/flutter (28363): #0 _JsonStringifier.writeObject (dart:convert/json.dart:688:7)
E/flutter (28363): #1 _JsonStringifier.writeMap (dart:convert/json.dart:769:7)
E/flutter (28363): #2 _JsonStringifier.writeJsonValue (dart:convert/json.dart:724:21)
E/flutter (28363): #3 _JsonStringifier.writeObject (dart:convert/json.dart:679:9)
E/flutter (28363): #4 _JsonStringStringifier.printOn (dart:convert/json.dart:877:17)
E/flutter (28363): #5 _JsonStringStringifier.stringify (dart:convert/json.dart:862:5)
E/flutter (28363): #6 JsonEncoder.convert (dart:convert/json.dart:262:30)
E/flutter (28363): #7 JsonCodec.encode (dart:convert/json.dart:172:45)
E/flutter (28363): #8 JsonConverter.encodeJson
E/flutter (28363): #9 JsonConverter.convertRequest
E/flutter (28363): #10 ChopperClient._encodeRequest
E/flutter (28363): #11 ChopperClient._handleRequestConverter
E/flutter (28363): #12 ChopperClient.send
E/flutter (28363): #13 _$PersonalPostingService.postPersonal
E/flutter (28363): #14 _FormState.build.<anonymous closure>.<anonymous closure>
E/flutter (28363): #15 _FormState.build.<anonymous closure>.<anonymous closure>
E/flutter (28363): #16 _InkResponseState._handleTap
E/flutter (28363): #17 GestureRecognizer.invokeCallback
E/flutter (28363): #18 TapGestureRecognizer.handleTapUp
E/flutter (28363): #19 BaseTapGestureRecognizer._checkUp
E/flutter (28363): #20 BaseTapGestureRecognizer.acceptGesture
E/flutter (28363): #21 GestureArenaManager.sweep
E/flutter (28363): #22 GestureBinding.handleEvent
E/flutter (28363): #23 GestureBinding.dispatchEvent
E/flutter (28363): #24 RendererBinding.dispatchEvent
E/flutter (28363): #25 GestureBinding._handlePointerEventImmediately
E/flutter (28363): #26 GestureBinding.handlePointerEvent
E/flutter (28363): #27 GestureBinding._flushPointerEventQueue
E/flutter (28363): #28 GestureBinding._handlePointerDataPacket
E/flutter (28363): #29 _rootRunUnary (dart:async/zone.dart:1370:13)
E/flutter (28363): #30 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter (28363): #31 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)
E/flutter (28363): #32 _invoke1 (dart:ui/hooks.dart:182:10)
E/flutter (28363): #33 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:282:7)
E/flutter (28363): #34 _dispatchPointerDataPacket (dart:ui/hooks.dart:96:31)
E/flutter (28363):
Exited (1)
The following is my onPressed() method of the button
The form is just useless sitting there I am using hardcoded values in a map from another class
child: RaisedButton(
onPressed: () async {
final form = _formKey.currentState;
if (form!.validate()) {
form.save();
//_user.makePostRequest();
showNotification();
await Provider.of<PersonalPostingService>(
context,
listen: false)
.postPersonal(_user.data);
}
},
child: Text('Save'))),

Unhandled Exception: Invalid argument(s): Illegal length 0 on DART

when i try to make a simple select with mysql1 dart library, the app throws this exception:
E/flutter (11252): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: Invalid argument(s): Illegal length 0
E/flutter (11252): #0 _NativeSocket.read (dart:io-patch/socket_patch.dart:819:7)
E/flutter (11252): #1 _RawSocket.read (dart:io-patch/socket_patch.dart:1614:22)
E/flutter (11252): #2 Buffer.readFromSocket (package:mysql1/src/buffer.dart:48:30)
E/flutter (11252): #3 BufferedSocket._readBuffer (package:mysql1/src/buffered_socket.dart:178:36)
E/flutter (11252): #4 BufferedSocket._onData (package:mysql1/src/buffered_socket.dart:93:9)
E/flutter (11252): #5 _rootRunUnary (dart:async/zone.dart:1206:13)
E/flutter (11252): #6 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (11252): #7 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter (11252): #8 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter (11252): #9 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:285:7)
E/flutter (11252): #10 _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:808:19)
E/flutter (11252): #11 _StreamController._add (dart:async/stream_controller.dart:682:7)
E/flutter (11252): #12 _StreamController.add (dart:async/stream_controller.dart:624:5)
E/flutter (11252): #13 new _RawSocket.<anonymous closure> (dart:io-patch/socket_patch.dart:1556:33)
E/flutter (11252): #14 _NativeSocket.issueReadEvent.issue (dart:io-patch/socket_patch.dart:1052:14)
E/flutter (11252): #15 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (11252): #16 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter (11252):
this is the function that makes the query (I call that function in a FutureBuilder in flutter):
Future<bool> askPlacaTurno(String idServicio)async{
var conexion = await conectarse();
var res = await conexion.query("select placa from servicios where id = ?", [idServicio]);
bool usaPlaca = false;
for(var row in res){
usaPlaca = row[0] == 'on' ? true : false;
}
return usaPlaca;
}
EDITED
the connection and close connection functions, here is the problem, i've tried to make a connection without execute any query and throws the same exception:
Future conectarse() async{
final conexion = await MySqlConnection.connect(ConnectionSettings(
host: "192.168.0.18",
port: 3306,
user: "root",
password: '',
db: "gobernacion"
)
);
print("conectado a la base de datos");
return conexion;
}
Future desconectarse(MySqlConnection conexion) async{
await conexion.close();
}

Warning during google places api implementation (Flutter)

So I am trying to add the Google Places API inside the code of my application, but I am getting these errors:
E/flutter (18565): #4 _rootRun (dart:async/zone.dart:1122:38)
E/flutter (18565): #5 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (18565): #6 _CustomZone.runGuarded (dart:async/zone.dart:925:7)
E/flutter (18565): #7 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
E/flutter (18565): #8 _rootRun (dart:async/zone.dart:1126:13)
E/flutter (18565): #9 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (18565): #10 _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:949:23)
E/flutter (18565): #11 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:23:15)
E/flutter (18565): #12 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
E/flutter (18565): #13 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
E/flutter (18565): #14 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
Some of my code:
Future<void> fetchAddress() async {
Prediction prediction =
await PlacesAutocomplete.show(
context: context,
onError: onError,
apiKey: 'dummy',
mode: Mode.fullscreen, // Mode.overlay
language: "en");
setState(() {
selectedAddress1 = prediction.description;
});
}

Flutter json.decode issue <!doctype html> error

I am trying to integrate stripe payment in my Flutter project with backend Laravel. For some reason I am getting error at json.decode.
E/flutter (10999): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)]
Unhandled Exception: FormatException: Unexpected character (at
character 1) E/flutter (10999): <!doctype html>
FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7.0)),
color: Colors.redAccent,
onPressed: () {
var payment = Payment();
payment.name = _cardHolderName.text;
payment.email = _cardHolderEmail.text;
payment.cardNumber = _cardNumber.text;
payment.expiryMonth = _expiryMonth.text;
payment.expiryYear = _expiryYear.text;
payment.cvcNumber = _cvcNumber.text;
_makePayment(context, payment);
},
child: Text('Make Payment',
style: TextStyle(color: Colors.white)),
),
void _makePayment(BuildContext context, Payment payment) async {
PaymentService _paymentService = PaymentService();
var paymentData = await _paymentService.makePayment(payment);
var result = json.decode(paymentData.body);
if (result['result'] == true) {
CartService _cartService = CartService();
this.widget.cartItems.forEach((cartItem){
_cartService.deleteCartItemById(cartItem.productId);
});
_showPaymentSuccessMessage(context);
Timer(Duration(seconds: 2), () {
Navigator.pop(context);
Navigator.push(
context, MaterialPageRoute(builder: (context) => HomeScreen()));
});
}
}
Payment Service
class PaymentService {
Repository _repository;
PaymentService(){
_repository = Repository();
}
makePayment(Payment payment) async {
return await _repository.httpPost('make-payment', payment.toJson());
}
}
Laravel Code
public function makePayment(Request $request)
{
\Stripe\Stripe::setApiKey('sk_test_7422pw1lMsdRfN2FaPmfFKlt00AtiIJD9O');
$token = Token::create([
'card'=>[
'number'=>$request-input('cardNumber'),
'exp_month'=>$request-input('expiryMonth'),
'exp_year'=>$request-input('expiryYear'),
'cvc'=>$request-input('cvcNumber')
]
]);
\Stripe\PaymentIntent::create([
'amount' => 1000,
'currency' => 'inr',
'source' => $token,
'receipt_email' => $request->input('email'),
]);
}
Error
E/flutter (10999): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter (10999): <!doctype html>
E/flutter (10999): ^
E/flutter (10999):
E/flutter (10999): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1394:5)
E/flutter (10999): #1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1261:9)
E/flutter (10999): #2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:926:22)
E/flutter (10999): #3 _parseJson (dart:convert-patch/convert_patch.dart:31:10)
E/flutter (10999): #4 JsonDecoder.convert (dart:convert/json.dart:495:36)
E/flutter (10999): #5 JsonCodec.decode (dart:convert/json.dart:153:41)
E/flutter (10999): #6 _PaymentScreenState._makePayment (package:factory2homes/screens/payment_screen.dart:160:23)
E/flutter (10999): <asynchronous suspension>
E/flutter (10999): #7 _PaymentScreenState.build.<anonymous closure> (package:factory2homes/screens/payment_screen.dart:142:23)
E/flutter (10999): #8 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
E/flutter (10999): #9 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
E/flutter (10999): #10 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (10999): #11 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
E/flutter (10999): #12 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
E/flutter (10999): #13 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:236:7)
E/flutter (10999): #14 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter (10999): #15 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20)
E/flutter (10999): #16 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (10999): #17 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (10999): #18 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (10999): #19 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (10999): #20 _rootRunUnary (dart:async/zone.dart:1138:13)
E/flutter (10999): #21 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (10999): #22 _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter (10999): #23 _invoke1 (dart:ui/hooks.dart:273:10)
E/flutter (10999): #24 _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)
E/flutter (10999):
This error happens when the server return an html body or some error occurred on the server side while handling your request. Try to use debugger and watch the response of the request to see if response status is 200 and also check the response body to see what the server is sending for your request.
Also try to wrap you jsonDecode inside try-catch to handle future exceptions.

google map palace flutter

I'm trying to fetch nearby places in my app, but there is some error.
If there is same topic please notice me
I'm looking forward for your advice
void _searchNearby(double latitude, double longitude) async {
setState(() {
markers.clear();
});
String url =
'${googlePlaceURL}/json?location=${latitude},${longitude}&radius=1500&type=${placeType}&key=${mapAPI}';
print(url);
http.Response response = await http.get(url);
print('********'+'$response');
if (response.statusCode == 200) {
final data = json.decode(response.body);
print(data);
} else {
throw Exception('An error occurred getting places nearby');
}
}
Error:
I/flutter (10208): https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=37.4219983,-122.084&radius=1500&type=subway_station&key=AIzaSyAjclu-U5mgWji4rENX0LxvrfYXljfK5pg
E/flutter (10208): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FormatException: Scheme not starting with alphabetic character (at character 1)
E/flutter (10208): https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=37.4...
E/flutter (10208): ^
E/flutter (10208):
E/flutter (10208): #0 _Uri._fail (dart:core/uri.dart:1623:5)
E/flutter (10208): #1 _Uri._makeScheme (dart:core/uri.dart:2184:7)
E/flutter (10208): #2 new _Uri.notSimple (dart:core/uri.dart:1465:18)
E/flutter (10208): #3 Uri.parse (dart:core/uri.dart:1022:17)
E/flutter (10208): #4 BaseClient._sendUnstreamed (package:http/src/base_client.dart:152:34)
E/flutter (10208): <asynchronous suspension>
E/flutter (10208): #5 BaseClient.get (package:http/src/base_client.dart:32:7)
E/flutter (10208): #6 get.<anonymous closure> (package:http/http.dart:46:36)
E/flutter (10208): #7 _withClient (package:http/http.dart:166:20)
E/flutter (10208): <asynchronous suspension>
E/flutter (10208): #8 get (package:http/http.dart:46:5)
E/flutter (10208): #9 MapSampleState._searchNearby (package:flutter_course/Map/map.dart:44:36)
E/flutter (10208): <asynchronous suspension>
E/flutter (10208): #10 MapSampleState.build.<anonymous closure> (package:flutter_course/Map/map.dart:119:11)
It looks like the variable googlePlaceURL start with space. Check it out, I think you can solve it in an easy way.