тип 'int' не является подтипом типа 'String' при приведении типа при отправке запроса на публикацию. - PullRequest
0 голосов
/ 21 января 2019

У меня появляется странная ошибка при отправке почтового запроса.

// 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.

1 Ответ

0 голосов
/ 21 января 2019

Чтобы решить вашу проблему, вам просто нужно закодировать данные перед отправкой.

 import 'dart:convert';
 ...

 final http.Response response =
    await http.post(Uri.encodeFull(url), body: json.encode(activityData));

Если ваш API не поддерживает JSON, тогда вам просто нужно передать все ваши данные как String.

    final Map<String, dynamic> activityData = {
      "user_id": "1",
      "name": activityName.text,
      "description": activityDescription.text,
      "startAt": activityStartAt.text,
      "endsAt": activityEndAt.text,
      "lat": "$_latitude",
      "long": "$_longitude",
      "category": "2",
      "status": "pending"
    };

     final http.Response response =
    await http.post(Uri.encodeFull(url), body: activityData);
...