颤动:使用"包崩溃:http / http.dart"在iOS模拟器上?

时间:2018-03-21 11:42:40

标签: ios ios-simulator flutter

我在Android Studio中使用Flutter beta 0.15v。崩溃只发生在iOS模拟器上 - IphoneX 11.2。

Unhandled exception:
NoSuchMethodError: The getter 'stdout' was called on null.
Receiver: null
Tried calling: stdout
#0      Object.noSuchMethod (dart:core-patch/dart:core/object_patch.dart:46)
#1      stdout (package:flutter_tools/src/base/io.dart:176)
#2      StdoutLogger.writeToStdOut (package:flutter_tools/src/base/logger.dart:100)
#3      StdoutLogger.printStatus (package:flutter_tools/src/base/logger.dart:95)
#4      _AppRunLogger.printStatus (package:flutter_tools/src/commands/daemon.dart:849)
#5      printStatus (package:flutter_tools/src/globals.dart:39)
#6      ResidentRunner._serviceDisconnected (package:flutter_tools/src/resident_runner.dart:756)
#7      _rootRun (dart:async/zone.dart:1122)
#8      _CustomZone.run (dart:async/zone.dart:1023)
#9      _FutureListener.handleWhenComplete (dart:async/future_impl.dart:151)
#10     _Future._propagateToListeners.handleWhenCompleteCallback (dart:async/future_impl.dart:603)
#11     _Future._propagateToListeners (dart:async/future_impl.dart:659)
#12     _Future._completeWithValue (dart:async/future_impl.dart:477)
#13     _Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:507)
#14     _rootRun (dart:async/zone.dart:1126)
#15     _CustomZone.run (dart:async/zone.dart:1023)
#16     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:949)
#17     _microtaskLoop (dart:async/schedule_microtask.dart:41)
#18     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50)
#19     _runPendingImmediateCallback (dart:isolate-patch/dart:isolate/isolate_patch.dart:113)
#20     _RawReceivePortImpl._handleMessage (dart:isolate-patch/dart:isolate/isolate_patch.dart:166)

使用http.dart

的API调用代码如下
 import 'package:http/http.dart' as http;

              _loadData() async {
            String dataURL = "https://api.github.com/orgs/abc/members";
            http.Response response = await http.get(dataURL);
            setState(() {
              final membersJSON = JSON.decode(response.body);

              for (var memberJSON in membersJSON) {
                final member = new Member(
                    memberJSON["login"], memberJSON["avatar_url"]);
                _members.add(member);
              }
            });
          }

在ios模拟器上运行时,每次都会抛出上述异常。

会员数据类

class Member {
  final String login;
  final String avatarUrl;

  Member(this.login, this.avatarUrl) {
    if (login == null) {
      throw new ArgumentError("login of Member cannot be null. "
          "Received: '$login'");
    }
    if (avatarUrl == null) {
      throw new ArgumentError("avatarUrl of Member cannot be null. "
          "Received: '$avatarUrl'");
    }
  }
}

2 个答案:

答案 0 :(得分:1)

我已尝试使用此代码,并且在使用iOS 11.2的iPhone X上运行正常。

_loadData() async {
String dataURL = "https://api.github.com/orgs/raywenderlich/members";
http.Response response = await http.get(dataURL);
setState(() {
  final membersJSON = JSON.decode(response.body);

  for (var memberJSON in membersJSON) {
    final member = new Member(
        memberJSON["login"], memberJSON["avatar_url"]);
   //_members.add(member);
    debugPrint("${member.login}");
  }
});

}

错误位于与HTTPClientRequest一起使用的dart.io库中,例外位于第176行here

我在http上使用iOS emulator时没有错误。所以我想问题出在您的SDK版本或您要解析的数据中。或者你的模拟器出了问题......你试过真正的设备吗?

这是我的扑动版:

Flutter 0.1.5 • channel beta • https://github.com/flutter/flutter.git
Framework • revision 3ea4d06340 (5 weeks ago) • 2018-02-22 11:12:39 -0800
Engine • revision ead227f118
Tools • Dart 2.0.0-dev.28.0.flutter-0b4f01f759

您可以尝试我的示例项目here

请尝试一下,让我知道它是否有效或你得到同样的错误。

此外输入flutter --version并粘贴您的输出。

答案 1 :(得分:0)

在新项目中再次创建整个演示后问题得到解决。可能问题只在于ios的构建过程,因为旧项目的android对应项正确执行。这个恼人的问题将在未来的更新中得到解决,我希望。