I am posting a map on button click using provider, which calls Chopper service to post a map data to the server. It gives an error to the server which is something like this.
I/flutter ( 4801): Observatory listening on ************************************
E/flutter ( 4801): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Converting object to an encodable object failed: Instance of 'DateTime'
E/flutter ( 4801): #0 _JsonStringifier.writeObject (dart:convert/json.dart:688:7)
E/flutter ( 4801): #1 _JsonStringifier.writeMap (dart:convert/json.dart:769:7)
E/flutter ( 4801): #2 _JsonStringifier.writeJsonValue (dart:convert/json.dart:724:21)
E/flutter ( 4801): #3 _JsonStringifier.writeObject (dart:convert/json.dart:679:9)
E/flutter ( 4801): #4 _JsonStringStringifier.printOn (dart:convert/json.dart:877:17)
E/flutter ( 4801): #5 _JsonStringStringifier.stringify (dart:convert/json.dart:862:5)
E/flutter ( 4801): #6 JsonEncoder.convert (dart:convert/json.dart:262:30)
E/flutter ( 4801): #7 JsonCodec.encode (dart:convert/json.dart:172:45)
E/flutter ( 4801): #8 JsonConverter.encodeJson
E/flutter ( 4801): #9 JsonConverter.convertRequest
E/flutter ( 4801): #10 ChopperClient._encodeRequest
E/flutter ( 4801): #11 ChopperClient._handleRequestConverter
E/flutter ( 4801): #12 ChopperClient.send
E/flutter ( 4801): #13 _$PersonalPostingService.postPersonal
E/flutter ( 4801): #14 _FormState.build.<anonymous closure>.<anonymous closure>
E/flutter ( 4801): #15 _FormState.build.<anonymous closure>.<anonymous closure>
E/flutter ( 4801): #33 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:282:7)
E/flutter ( 4801): #34 _dispatchPointerDataPacket (dart:ui/hooks.dart:96:31)
E/flutter ( 4801):
Exited (1)
This is where I am calling the function the body is a MAP of string, dynamic
child: RaisedButton(
onPressed: () async {
final form = _formKey.currentState;
if (form!.validate()) {
form.save();
showNotification();
await Provider.of<PersonalPostingService>(
context,
listen: false)
.postPersonal(data);
}
},
child: Text('Save'))),
The GET requests are working fine and are easily being displayed in a listview but the POST function is not working, just for the reference below is the chopper service for POST
#Post(path: 'http://157.......:8040/personal')
Future<Response> postPersonal(
#Body() Map<String, dynamic> body,
);
Related
My app was working fine on both iOS and Android but after updating to Flutter 2.10.0 I'm getting this issue:
Unhandled Exception: Null check operator used on a null value
E/flutter ( 8991): #0 Element.widget (package:flutter/src/widgets/framework.dart:3203:31)
E/flutter ( 8991): #1 Provider._inheritedElementOf (package:provider/src/provider.dart:341:17)
E/flutter ( 8991): #2 Provider.of (package:provider/src/provider.dart:293:30)
E/flutter ( 8991): #3 Shop.getPastPurchases.<anonymous closure> (package:my_rents/services/shop.dart:123:20)
E/flutter ( 8991): #4 _rootRunUnary (dart:async/zone.dart:1434:47)
E/flutter ( 8991): #5 _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 8991): #6 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
E/flutter ( 8991): #7 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
E/flutter ( 8991): #8 _DelayedData.perform (dart:async/stream_impl.dart:591:14)
E/flutter ( 8991): #9 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:706:11)
E/flutter ( 8991): #10 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:663:7)
E/flutter ( 8991): #11 _rootRun (dart:async/zone.dart:1418:47)
E/flutter ( 8991): #12 _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8991): #13 _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter ( 8991): #14 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter ( 8991): #15 _rootRun (dart:async/zone.dart:1426:13)
E/flutter ( 8991): #16 _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8991): #17 _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter ( 8991): #18 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter ( 8991): #19 _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
E/flutter ( 8991): #20 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
My implementation:
Future<List<PurchaseDetails>> getPastPurchases(BuildContext context) async {
if (this.purchases.isEmpty) {
final Stream<List<PurchaseDetails>> purchaseUpdated =
inAppPurchase.purchaseStream;
_subscription = purchaseUpdated.listen((purchaseDetailsList) {
if (purchaseDetailsList.isEmpty) {
Provider.of<AdState>(context, listen: false).toggleAds(context, true);
} else {
listenToPurchaseUpdated(context, purchaseDetailsList);
}
}, onDone: () {
_subscription.cancel();
}, onError: (error) {
});
await inAppPurchase.restorePurchases(); // If I comment this line everything works
}
return this.purchases;
}
Again, I made no changes on my code, just updating Flutter
I'm getting data from my api as a following JSON object to create chart with the data. I want to covert this DIO response to a Map<String, int> object in my flutter app.
My API response:
{
"November 20": 1,
"October 20": 3,
"September 20": 1
}
My try of creating a map object:
Future<bool> fetchChartData() async {
var response = await CasesApiService().getChartData();
Map<String, dynamic> data = jsonDecode(response.data);
}
But it shows the following error:
E/flutter ( 4733): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
E/flutter ( 4733): #0 CasesProvider.fetchChartData
package:bdopsApp/providers/casesProvider.dart:86
E/flutter ( 4733): <asynchronous suspension>
E/flutter ( 4733): #1 _CasesChartViewState.build.<anonymous closure>
package:bdopsApp/…/casesApp/casesChart.dart:31
E/flutter ( 4733): #2 _InkResponseState._handleTap
package:flutter/…/material/ink_well.dart:985
E/flutter ( 4733): #3 _InkResponseState.build.<anonymous closure>
package:flutter/…/material/ink_well.dart:1101
E/flutter ( 4733): #4 GestureRecognizer.invokeCallback
package:flutter/…/gestures/recognizer.dart:183
E/flutter ( 4733): #5 TapGestureRecognizer.handleTapUp
package:flutter/…/gestures/tap.dart:598
E/flutter ( 4733): #6 BaseTapGestureRecognizer._checkUp
package:flutter/…/gestures/tap.dart:287
E/flutter ( 4733): #7 BaseTapGestureRecognizer.handlePrimaryPointer
package:flutter/…/gestures/tap.dart:222
E/flutter ( 4733): #8 PrimaryPointerGestureRecognizer.handleEvent
package:flutter/…/gestures/recognizer.dart:476
E/flutter ( 4733): #9 PointerRouter._dispatch
package:flutter/…/gestures/pointer_router.dart:77
E/flutter ( 4733): #10 PointerRouter._dispatchEventToRoutes.<anonymous closure>
package:flutter/…/gestures/pointer_router.dart:122
E/flutter ( 4733): #11 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8)
E/flutter ( 4733): #12 PointerRouter._dispatchEventToRoutes
package:flutter/…/gestures/pointer_router.dart:120
E/flutter ( 4733): #13 PointerRouter.route
package:flutter/…/gestures/pointer_router.dart:106
E/flutter ( 4733): #14 GestureBinding.handleEvent
package:flutter/…/gestures/binding.dart:368
E/flutter ( 4733): #15 GestureBinding.dispatchEvent
package:flutter/…/gestures/binding.dart:348
E/flutter ( 4733): #16 RendererBinding.dispatchEvent
package:flutter/…/rendering/binding.dart:268
E/flutter ( 4733): #17 GestureBinding._handlePointerEventImmediately
package:flutter/…/gestures/binding.dart:303
E/flutter ( 4733): #18 GestureBinding.handlePointerEvent
package:flutter/…/gestures/binding.dart:267
E/flutter ( 4733): #19 GestureBinding._flushPointerEventQueue
package:flutter/…/gestures/binding.dart:225
E/flutter ( 4733): #20 GestureBinding._handlePointerDataPacket
package:flutter/…/gestures/binding.dart:208
E/flutter ( 4733): #21 _rootRunUnary (dart:async/zone.dart:1206:13)
E/flutter ( 4733): #22 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 4733): #23 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter ( 4733): #24 _invoke1 (dart:ui/hooks.dart:265:10)
E/flutter ( 4733): #25 _dispatchPointerDataPacket (dart:ui/hooks.dart:174:5)
N.B: I'm doing my final year project. Therefore, the process I'm following might not be what is used in real world project.
If you run the following example in the dartPad you will get the result that you are looking for:
import 'dart:convert';
void main() {
var jsonString= "{\"November 20\": \"1\",\"October 20\": \"2\",\"September 20\": \"3\"}";
Map<String, dynamic> data = jsonDecode(jsonString);
print(data);
}
I think you need to check the data that you are getting it from the response.data
I have a query function for data from the database that comes with the id number from the database, then the id is saved in shared_preferences in case the new id that comes from the database is larger than the old id that was in the shared_preferences previously.But after running the code, the following problem comes:
E/flutter (30884): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: Class 'String' has no instance method '<'.
E/flutter (30884): Receiver: ""
E/flutter (30884): Tried calling: <(7)
E/flutter (30884): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
E/flutter (30884): #1 AddCommentsState.getLogin.<anonymous closure> (package:flutter_apptestqeuriy/AddComment.dart:84:46)
E/flutter (30884): #2 State.setState (package:flutter/src/widgets/framework.dart:1240:30)
E/flutter (30884): #3 AddCommentsState.getLogin (package:flutter_apptestqeuriy/AddComment.dart:78:5)
E/flutter (30884): <asynchronous suspension>
E/flutter (30884): #4 AddCommentsState.initState.<anonymous closure> (package:flutter_apptestqeuriy/AddComment.dart:57:9)
E/flutter (30884): #5 interval.function (package:flutter_apptestqeuriy/AddComment.dart:21:9)
E/flutter (30884): #6 _rootRun (dart:async/zone.dart:1182:47)
E/flutter (30884): #7 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (30884): #8 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (30884): #9 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (30884): #10 _rootRun (dart:async/zone.dart:1190:13)
E/flutter (30884): #11 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (30884): #12 _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
E/flutter (30884): #13 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter (30884): #14 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
E/flutter (30884): #15 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
E/flutter (30884): #16 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter (30884):
This is the code used to query from the database:
var user_id;
var my_pre_id;
Future<String> getLogin() async {
var response = await http.get(Uri.encodeFull("http://xxxxxxxxxxx/Application.php"),);
setState(() {
var convertDataToJson = json.decode(response.body);
data = convertDataToJson['result'];
if (data != null) {
user_id = int.parse(data[0]['id']);
my_pre_id = _myPreferences.id;
if (my_pre_id == null || my_pre_id < user_id ){
_myPreferences.id = user_id;
_myPreferences.commit();
// print("tappeeeeedddd $my_pre_id");
}
}
});
}
Does anyone know the cause of the problem?
I think the issue comes from my_pre_id < user_id where my_pre_id is actually a String. You might want to do an int.parse() on it as well or change its type in the class.
Here is my Client code and server is in c++. But when i am trying to connect this client to server it is throwing an error that websocket is not upgrading. The server is running i have checked with js client.
Code for client
import 'package:socketcluster_client/socketcluster_client.dart';
import 'dart:async';
class MyListener extends BasicListener {
#override
void onAuthentication(Socket socket, bool status) {
print('onAuthentication: socket $socket status $status');
}
#override
void onConnectError(Socket socket, e) {
print('onConnectError: socket $socket e $e');
}
#override
void onConnected(Socket socket) {
print('onConnected: socket $socket');
new Timer.periodic(const Duration(seconds: 2), (_) {
print('Attempting to send');
socket.emit('sampleClientEvent',
{'message': 'This is an object with a message property'});
});
}
#override
void onDisconnected(Socket socket) {
print('onDisconnected: socket $socket');
}
#override
void onSetAuthToken(String token, Socket socket) {
print('onSetAuthToken: socket $socket token $token');
socket.authToken = token;
}
}
main() async {
var socket = await Socket.connect('ws://10.5.11.88:7777/socketcluster/',
listener: new MyListener());
socket.on('rand', (name, data, ack) {
print('got message $data from event $name');
ack(name, 'No error', 'Hi there buddy');
});
}
I have tried https://github.com/flutter/flutter/issues/11444 but it is not working.
Error Log
Launching lib\main.dart on AOSP on IA Emulator in debug mode...
Running Gradle task 'assembleDebug'...
√ Built build\app\outputs\apk\debug\app-debug.apk.
Installing build\app\outputs\apk\app.apk...
Syncing files to device AOSP on IA Emulator...
E/flutter ( 5399): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled
Exception: WebSocketException: Connection to
'http://10.5.11.88:7777/socketcluster/#' was not upgraded to websocket
E/flutter ( 5399): #0 _WebSocketImpl.connect.<anonymous closure>.error
(dart:_http/websocket_impl.dart:1054:9)
E/flutter ( 5399): #1 _WebSocketImpl.connect.<anonymous closure>
(dart:_http/websocket_impl.dart:1063:14)
E/flutter ( 5399): #2 _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter ( 5399): #3 _CustomZone.runUnary
(dart:async/zone.dart:1031:19)
E/flutter ( 5399): #4 _FutureListener.handleValue
(dart:async/future_impl.dart:139:18)
E/flutter ( 5399): #5 Future._propagateToListeners.handleValueCallback
(dart:async/future_impl.dart:680:45)
E/flutter ( 5399): #6 Future._propagateToListeners
(dart:async/future_impl.dart:709:32)
E/flutter ( 5399): #7 Future._completeWithValue
(dart:async/future_impl.dart:524:5)
E/flutter ( 5399): #8 Future.wait.<anonymous closure>
(dart:async/future.dart:400:22)
E/flutter ( 5399): #9 _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter ( 5399): #10 _CustomZone.runUnary
(dart:async/zone.dart:1031:19)
E/flutter ( 5399): #11 _FutureListener.handleValue
(dart:async/future_impl.dart:139:18)
E/flutter ( 5399): #12 Future._propagateToListeners.handleValueCallback
(dart:async/future_impl.dart:680:45)
E/flutter ( 5399): #13 Future._propagateToListeners
(dart:async/future_impl.dart:709:32)
E/flutter ( 5399): #14 Future._completeWithValue
(dart:async/future_impl.dart:524:5)
E/flutter ( 5399): #15 Future._asyncComplete.<anonymous closure>
(dart:async/future_impl.dart:554:7)
E/flutter ( 5399): #16 _rootRun (dart:async/zone.dart:1126:13)
E/flutter ( 5399): #17 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter ( 5399): #18 _CustomZone.runGuarded
(dart:async/zone.dart:925:7)
E/flutter ( 5399): #19 _CustomZone.bindCallbackGuarded.<anonymous
closure> (dart:async/zone.dart:965:23)
E/flutter ( 5399): #20 _microtaskLoop
(dart:async/schedule_microtask.dart:43:21)
E/flutter ( 5399): #21 _startMicrotaskLoop
(dart:async/schedule_microtask.dart:52:5)
E/flutter ( 5399):
Actually strict application load balancer policy was applied on AWS/server
Relaxing it solved the issue
I'm trying to use a simple GET requests in REST to flutter, but have an error
Code:
Future<Map> timeStamp() async {
const request =
"https://armariosinteligentes.com/api/v3/timestamp";
http.Response response = await http.get(request);
print(json.decode(response.body));
}
json: {"timestamp":1566397501}
Error:
E/flutter ( 7041): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: HandshakeException: Handshake error in client (OS Error:
E/flutter ( 7041): BLOCK_TYPE_IS_NOT_01(padding.c:108)
E/flutter ( 7041): PADDING_CHECK_FAILED(rsa_impl.c:641)
E/flutter ( 7041): public key routines(a_verify.c:105)
E/flutter ( 7041): CERTIFICATE_VERIFY_FAILED: certificate signature failure(handshake.cc:352)) E/flutter ( 7041):
#0 IOClient.send (package:http/src/io_client.dart:33:23) E/flutter ( 7041): <asynchronous suspension> E/flutter ( 7041):
#1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:169:38) E/flutter ( 7041): <asynchronous suspension> E/flutter ( 7041):
#2 BaseClient.get (package:http/src/base_client.dart:32:7) E/flutter ( 7041):
#3 get.<anonymous closure> (package:http/http.dart:46:36) E/flutter ( 7041):
#4 _withClient (package:http/http.dart:166:20) E/flutter ( 7041): <asynchronous suspension> E/flutter ( 7041):
#5 get (package:http/http.dart:46:5) E/flutter ( 7041):
#6 timeStamp (package:armarios_inteligentes/screens/locker_screen.dart:196:34) E/flutter ( 7041): <asynchronous suspension> E/flutter ( 7041):
#7 LockerScreenState.build.<anonymous closure> (package:armarios_inteligentes/screens/locker_screen.dart:114:15) E/flutter ( 7041):
#8 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:635:14) E/flutter ( 7041):
#9 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:711:32) E/flutter ( 7041):
#10 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24) E/flutter ( 7041): #11 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11) E/flutter ( 7041):
#12 TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:312:7) E/flutter ( 7041):
#13 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27) E/flutter ( 7041):
#14 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20) E/flutter ( 7041):
#15 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22) E/flutter ( 7041):
#16 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7) E/flutter ( 7041):
#17 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7) E/flutter ( 7041):
#18 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7) E/flutter ( 7041):
#19 _rootRunUnary (dart:async/zone.dart:1136:13) E/flutter ( 7041):
#20 _CustomZone.runUnary (dart:async/zone.dart:1029:19) E/flutter ( 7041):
#21 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7) E/flutter ( 7041):
#22 _invoke1 (dart:ui/hooks.dart:250:10) E/flutter ( 7041):
#23 _dispatchPointerDataPacket (dart:ui/hooks.dart:159:5) E/flutter ( 7041):
timeStamp() async {
final response =
await http.get('http://armariosinteligentes.com/api/v3/timestamp');
if (response.statusCode == 200) {
// If server returns an OK response, parse the JSON.
var jsonResponse = json.decode(response.body);
tempoStamp tempo = new tempoStamp.fromJson(jsonResponse);
var time = ('${tempo.timestamp}');
return time;
} else {
// If that response was not OK, throw an error.
throw Exception('Failed to load post');
}
}
Result: 1566411603
Try to remove the https and put http instead. And verify your connection, you need to know if you can call websites like this in your local...