NoSuchMethodError:在 null 上调用了 getter 'length'。在调用 put api 时?

时间:2021-05-13 16:25:22

标签: api flutter restapi

我在处理 flutter 时遇到一个问题,每当我尝试调用 http.put 请求时都会出现奇怪的问题。

这是我的主要代码。

saveBookMarksLogin(id) async {
    final prefs = await SharedPreferences.getInstance();
    var bookmarkData = {
      "userId": userId,
      "newsFeedId": id,
    };
    try {
      final response = await http.put(
        Uri(
          scheme: 'http',
          host: host,
          port: 3000,
          path: '/v1/saveBookmark',
        ),
        body: bookmarkData,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
      );

      if (response.body != null) {
        prefs.setStringList("bookmarkIds", [id]);
        return json.decode(response.body);
      }
    } on SocketException {
      Fluttertoast.showToast(
        msg: "No internet connection",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIos: 1,
        textColor: Colors.white,
        fontSize: 16.0,
        backgroundColor: Colors.black,
      );
    }
  }

调试时问题的堆栈跟踪..

I/flutter (23979): error NoSuchMethodError: The getter 'length' was called on null.
I/flutter (23979): Receiver: null
I/flutter (23979): Tried calling: length
I/flutter (23979): stacktrace #0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:51:5)
I/flutter (23979): #1      _Uri._uriEncode  (dart:core-patch/uri_patch.dart:44:23)
I/flutter (23979): #2      Uri.encodeQueryComponent  (dart:core/uri.dart:1103:17)
I/flutter (23979): #3      mapToQuery.<anonymous closure> 
package:http/src/utils.dart:19
I/flutter (23979): #4      CastMap.forEach.<anonymous closure>  (dart:_internal/cast.dart:286:8)
I/flutter (23979): #5      _LinkedHashMapMixin.forEach  (dart:collection-patch/compact_hash.dart:377:8)
I/flutter (23979): #6      CastMap.forEach  (dart:_internal/cast.dart:285:13)
I/flutter (23979): #7      mapToQuery 
package:http/src/utils.dart:17
I/flutter (23979): #8      Request.bodyFields= 
package:http/src/request.dart:128
I/flutter (23979): #9      BaseClient._sendUnstreamed 
package:http/src/base_client.dart:163
I/flutter (23979): <asynchronous suspension>
I/flutter (23979): #10     BaseClient.put 
package:http/src/base_client.dart:76
I/flutter (23979): #11     put.<anonymous closure> 
package:http/http.dart:94
I/flutter (23979): #12     _withClient 
package:http/http.dart:166
I/flutter (23979): <asynchronous suspension>
I/flutter (23979): #13     put 
package:http/http.dart:93
I/flutter (23979): #14     _NewsPageState.saveBookMarksLogin (package:i

颤振医生 -v

[√] Flutter (Channel stable, 2.0.6, on Microsoft Windows [Version 10.0.21359.1], locale en-IN) • Flutter 2.0.6 版位于 C:\Program Files\flutter • 框架修订版 1d9032c7e1(2 周前),2021-04-29 17:37:58 -0700 • 引擎版本 05e680e202 • Dart 版本 2.12.3

[√] Android 工具链 - 为 Android 设备开发(Android SDK 版本 30.0.3) • Android SDK 位于 C:\Users\lkrja\AppData\Local\Android\sdk • 平台 android-30,构建工具 30.0.3 • Java 二进制文件位于:C:\Program Files\Android\Android Studio\jre\bin\java • Java 版 OpenJDK 运行时环境(构建 11.0.8+10-b944.6842174) • 接受所有 Android 许可。

[√] Chrome - 为网络开发 • Chrome 位于 C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - 为 Windows 开发(Visual Studio Community 2019 16.9.3) • Visual Studio 位于 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community • Visual Studio Community 2019 版本 16.9.31129.286 • Windows 10 SDK 版本 10.0.19041.0

[√] Android Studio(4.1.0 版本) • Android Studio 位于 C:\Program Files\Android\Android Studio • Flutter 插件可以从以下位置安装: https://plugins.jetbrains.com/plugin/9212-flutter • Dart 插件可以从以下位置安装: https://plugins.jetbrains.com/plugin/6351-dart • Java 版 OpenJDK 运行时环境(构建 11.0.8+10-b944.6842174)

[√] VS Code(1.56.1 版本) • VS Code 位于 C:\Users\lkrja\AppData\Local\Programs\Microsoft VS Code • Flutter 扩展版本 3.22.0

[√] 已连接设备(4 个可用) • 为 x86(移动)构建的 Android SDK • emulator-5554 • android-x86 • Android 8.1.0 (API 27)(模拟器) • Windows(桌面) • windows • windows-x64 • Microsoft Windows [版本 10.0.21359.1] • Chrome (web) • chrome • web-javascript • Google Chrome 90.0.4430.212 • Edge (web) • edge • web-javascript • Microsoft Edge 90.0.818.56

• 未发现问题!

0 个答案:

没有答案