在http中发个请求向API发帖

时间:2019-05-13 18:15:17

标签: api http post flutter

我想向api发送http发布请求,但我将http.post与flutter结合使用:

class SignUpApiProvider {
  Client client = Client();
  final host = Setting.host + 'signup';

  Future signUp(String name, String email, String password) async {
    final res = await client
        .post(host, body:{'name': name, 'email': email, 'password': password});
    if (res.statusCode == 200) {
     return res;
    } else {
      return throw Exception('Failed to add data');
    }
  }
}

当请求时给我这个错误:

E/flutter (17691): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Exception: Failed to add data
E/flutter (17691): #0      SignUpApiProvider.signUp (package:mosul/src/resources/SignupApiProvider.dart:19:14)
E/flutter (17691): <asynchronous suspension>
E/flutter (17691): #1      Repository.signUp (package:mosul/src/resources/repository.dart:14:83)
E/flutter (17691): #2      SignUpState.build.<anonymous closure> (package:mosul/src/ui/users/signup.dart:66:34)
E/flutter (17691): <asynchronous suspension>
E/flutter (17691): #3      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:513:14)
E/flutter (17691): #4      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:568:30)
E/flutter (17691): #5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:120:24)
E/flutter (17691): #6      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
E/flutter (17691): #7      TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:204:7)
E/flutter (17691): #8      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter (17691): #9      _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:20)
E/flutter (17691): #10     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:192:22)
E/flutter (17691): #11     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:149:7)
E/flutter (17691): #12     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
E/flutter (17691): #13     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
E/flutter (17691): #14     _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter (17691): #15     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (17691): #16     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (17691): #17     _invoke1 (dart:ui/hooks.dart:223:10)
E/flutter (17691): #18     _dispatchPointerDataPacket (dart:ui/hooks.dart:144:5)
E/flutter (17691):

当我尝试使用Postman发出请求时,效果很好,非常感谢

编辑:当我在其他部分打印(res.body)时:

{"message":"cannot create user!!"}
E/flutter ( 3026): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Exception: Failed to add data
.............

来自服务器 .... 但邮递员这是工作:( enter image description here

0 个答案:

没有答案