Flutter http错误类型'_InternalLinkedHashMap <dynamic,dynamic =“”>'不是类型'Map <string,string =“”>'的子类型

时间:2018-10-18 17:15:18

标签: dart flutter

我使用flutter http包创建了简单的发布请求。但是发送请求时我遇到了错误。

E/flutter ( 9479): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception:
E/flutter ( 9479): type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, String>'
E/flutter ( 9479): #0      ApiRequester.sendLoginRequest (package:fitness_lanka/api_requester.dart:56:16)
E/flutter ( 9479): <asynchronous suspension>
E/flutter ( 9479): #1      LoginWidgets.buildLoginButton.<anonymous closure> (package:fitness_lanka/widgets/login_widgets.dart:82:62)
E/flutter ( 9479): #2      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
E/flutter ( 9479): #3      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:562:30)
E/flutter ( 9479): #4      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter ( 9479): #5      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
E/flutter ( 9479): #6      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
E/flutter ( 9479): #7      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
E/flutter ( 9479): #8      PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
E/flutter ( 9479): #9      PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
E/flutter ( 9479): #10     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:143:19)
E/flutter ( 9479): #11     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
E/flutter ( 9479): #12     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
E/flutter ( 9479): #13     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
E/flutter ( 9479): #14     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
E/flutter ( 9479): #15     _invoke1 (dart:ui/hooks.dart:153:13)
E/flutter ( 9479): #16     _dispatchPointerDataPacket (dart:ui/hooks.dart:107:5)

下面是我用来发送帖子请求的功能。

static Future<LoginData> sendLoginRequest(
      String email, String password) async {
    var headers = {'Accept': 'application/json'};
    var body = {
      'email': email,
      'password': password,
    };
    http.Response response = await http.post(
      "https://fitness-lanka-laravel.herokuapp.com/api/user/login",
      headers: headers,
      body: body,
    );
    return LoginData.fromResponse(response);
  }

我将flutter从v0.8.2升级到v0.9.4。在将flutter升级到最新版本之前,它运行良好。

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, v0.9.4, on Microsoft Windows [Version 10.0.17134.345], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK 28.0.2)
[√] Android Studio (version 3.2)
[√] IntelliJ IDEA Community Edition (version 2018.2)
[√] VS Code (version 1.28.1)
[√] Connected devices (1 available)

• No issues found!

-----更新-----

我将flutter降级到v0.8.2。现在我遇到了另一个错误。

E/flutter (24516): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception:
E/flutter (24516): NoSuchMethodError: The getter 'length' was called on null.
E/flutter (24516): Receiver: null
E/flutter (24516): Tried calling: length
E/flutter (24516): #0      Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:48:5)
E/flutter (24516): #1      _Uri._uriEncode (dart:core/runtime/liburi_patch.dart:44:23)
E/flutter (24516): #2      Uri.encodeQueryComponent (dart:core/uri.dart:1092:17)
E/flutter (24516): #3      mapToQuery.<anonymous closure> (package:http/src/utils.dart:19:22)
E/flutter (24516): #4      CastMap.forEach.<anonymous closure> (dart:_internal/cast.dart:286:8)
E/flutter (24516): #5      __InternalLinkedHashMap&_HashVMBase&MapMixin&_LinkedHashMapMixin.forEach (dart:collection/runtime/libcompact_hash.dart:365:8)
E/flutter (24516): #6      CastMap.forEach (dart:_internal/cast.dart:285:13)
E/flutter (24516): #7      mapToQuery (package:http/src/utils.dart:17:7)
E/flutter (24516): #8      Request.bodyFields= (package:http/src/request.dart:128:17)
E/flutter (24516): #9      BaseClient._sendUnstreamed (package:http/src/base_client.dart:165:17)
E/flutter (24516): <asynchronous suspension>
E/flutter (24516): #10     BaseClient.post (package:http/src/base_client.dart:56:5)
E/flutter (24516): #11     post.<anonymous closure> (package:http/http.dart:70:34)
E/flutter (24516): #12     _withClient (package:http/http.dart:167:20)
E/flutter (24516): <asynchronous suspension>
E/flutter (24516): #13     post (package:http/http.dart:70:3)
E/flutter (24516): #14     ApiRequester.sendLoginRequest (package:fitness_lanka/api_requester.dart:55:36)
E/flutter (24516): <asynchronous suspension>
E/flutter (24516): #15     LoginWidgets.buildLoginButton.<anonymous closure> (package:fitness_lanka/widgets/login_widgets.dart:84:62)
E/flutter (24516): #16     _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:503:14)
E/flutter (24516): #17     _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:558:30)
E/flutter (24516): #18     GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter (24516): #19     TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
E/flutter (24516): #20     TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
E/flutter (24516): #21     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
E/flutter (24516): #22     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
E/flutter (24516): #23     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
E/flutter (24516): #24     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:143:19)
E/flutter (24516): #25     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
E/flutter (24516): #26     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
E/flutter (24516): #27     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
E/flutter (24516): #28     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
E/flutter (24516): #29     _invoke1 (dart:ui/hooks.dart:142:13)
E/flutter (24516): #30     _dispatchPointerDataPacket (dart:ui/hooks.dart:99:5)

1 个答案:

答案 0 :(得分:2)

以下代码对我有用:)

  static Future<LoginData> sendLoginRequest(
      String email, String password) async {
    // Encode the string [uri] using percent-encoding to make it
    // safe for literal use as a full URI.
    String url = Uri.encodeFull('https://fitness-lanka-laravel.herokuapp.com/api/user/login');

    // headers should be a subtype of Map<String, String>
    Map<String, String> headers = {
      "Accept": "application/json",
      "Content-Type": "application/json",
    };

    // convert body to a JSON string
    String body = json.encode(
      {
        'email': email,
        'password': password,
      },
    );

    // now it's safe to send the request
    http.Response response = await http.post(
      url,
      body: body,
      headers: headers,
    );
    return LoginData.fromResponse(response);
  }

如果有人在json.encode(body)遇到错误

请添加以下项的导入:import 'dart:convert';