web-dev-qa-db-ja.com

タイプ 'int'は、ポストリクエストを送信するときのタイプキャストのタイプ 'String'のサブタイプではありません

投稿リクエストを送信すると、奇妙なエラーが表示されます。

// this map is passed to a function
final Map<String, dynamic> activityData = {
  "userId": 1,
  "name": activityName.text,
  "description": activityDescription.text,
  "startAt": activityStartAt.text,
  "endsAt": activityEndAt.text,
  "lat": _latitude,
  "long": _longitude,
  "category": 2,
  "status": "pending"
};

// this code bellow is inside a async function
final http.Response response =
        await http.post(Uri.encodeFull(url), body: activityData);

これはエラーです:

E/flutter (32582): [ERROR:flutter/Shell/common/Shell.cc(184)] Dart Error: Unhandled exception:
E/flutter (32582): type 'int' is not a subtype of type 'String' in type cast
E/flutter (32582): #0      CastMap.forEach.<anonymous closure> (Dart:_internal/cast.Dart:286:25)
E/flutter (32582): #1      __InternalLinkedHashMap&_HashVMBase&MapMixin&_LinkedHashMapMixin.forEach (Dart:collection/runtime/libcompact_hash.Dart:370:8)
E/flutter (32582): #2      CastMap.forEach (Dart:_internal/cast.Dart:285:13)
E/flutter (32582): #3      mapToQuery (package:http/src/utils.Dart:17:7)
E/flutter (32582): #4      Request.bodyFields= (package:http/src/request.Dart:128:17)
E/flutter (32582): #5      BaseClient._sendUnstreamed (package:http/src/base_client.Dart:163:17)
E/flutter (32582): <asynchronous suspension>
E/flutter (32582): #6      BaseClient.post (package:http/src/base_client.Dart:54:7)
E/flutter (32582): #7      post.<anonymous closure> (package:http/http.Dart:70:16)
E/flutter (32582): #8      _withClient (package:http/http.Dart:166:20)
E/flutter (32582): <asynchronous suspension>
E/flutter (32582): #9      post (package:http/http.Dart:69:5)
E/flutter (32582): #10     _MainModel&Model&ConnectedModel&UsersModel&ActivitiesModel.createActivity (package:activmap/scoped-models/connectedModel.Dart:62:15)
E/flutter (32582): <asynchronous suspension>
E/flutter (32582): #11     _NewActivityState.save (package:activmap/pages/newActivity.Dart:271:11)
E/flutter (32582): <asynchronous suspension>
E/flutter (32582): #12     _NewActivityState.build.<anonymous closure> (package:activmap/pages/newActivity.Dart:248:30)
E/flutter (32582): #13     _InkResponseState._handleTap (package:flutter/src/material/ink_well.Dart:507:14)
E/flutter (32582): #14     _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.Dart:562:30)
E/flutter (32582): #15     GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.Dart:102:24)
E/flutter (32582): #16     TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.Dart:242:9)
E/flutter (32582): #17     TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.Dart:175:7)
E/flutter (32582): #18     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.Dart:315:9)
E/flutter (32582): #19     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.Dart:73:12)
E/flutter (32582): #20     PointerRouter.route (package:flutter/src/gestures/pointer_router.Dart:101:11)
E/flutter (32582): #21     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.Dart:180:19)
E/flutter (32582): #22     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.Dart:158:22)
E/flutter (32582): #23     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.Dart:138:7)
E/flutter (32582): #24     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.Dart:101:7)
E/flutter (32582): #25     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.Dart:85:7)
E/flutter (32582): #26     _invoke1 (Dart:ui/hooks.Dart:168:13)
E/flutter (32582): #27     _dispatchPointerDataPacket (Dart:ui/hooks.Dart:122:5)

私はそれが別の画面で正常に動作していることを確認できます唯一の違いは、動作しているものは文字列しかありませんが、これはint、double、Stringを持っていることです。

5

チェックアウト requests -httpリクエストを作成するフラッターライブラリ(pythonリクエストモジュールに触発された)

投稿application/x-www-form-urlencoded

var r = await Requests.post(
  "https://reqres.in/api/users", body: {
    "userId": 10,
    "id": 91,
    "title": "aut amet sed",
  },
  bodyEncoding: RequestBodyEncoding.FormURLEncoded);

r.raiseForStatus();
dynamic json = r.json();
print(json['id']);

投稿application/json

var r = await Requests.post("https://reqres.in/api/users", json: {
    "userId": 10,
    "id": 91,
    "title": "aut amet sed",
    "body": "libero voluptate eveniet aperiam sed\nsunt placeat suscipit molestias\nsimilique fugit nam natus\nexpedita consequatur consequatur dolores quia eos et placeat",
  });

r.raiseForStatus();
dynamic body = r.json();
print(json['id']);
0
Jossef Harush