在服务器上注册的Flutter简单实现无法获得任何响应

时间:2019-06-04 09:01:47

标签: flutter bloc

在Flutter中,我尝试通过api与bloc在服务器上实现简单注册,

当我尝试连接并将数据发送到服务器以接收响应时,我没有收到任何errormessage

RegisterRepository类:

class RegisterRepository {
  Future<RegisterResponse> register({@required String mobileNumber}) async {
    ModelRegister register = ModelRegister(mobile_number: mobileNumber);

    final response =
        await http.post(Constants.register, body: register.toJson());
    if (response.statusCode == 200) {
      final responseString = jsonDecode(response.body);

      return RegisterResponse.fromJson(responseString);
    }else{
      throw Exception('error on RegisterResponse');
    }
  }
}

注册表格:

TextFormField(
    controller: mobileNumber,
    keyboardType:
        TextInputType.number,
    validator: (String value) {
    },

    textInputAction:
        TextInputAction.next,
    ...
      hintText: "your mobile number",
      ...
)),

 GestureDetector(
   onTap: _onLoginButtonPressed,
   child: Container(
     child: Center(
       child: Text(
         "register",
         ),
     ),
   ),
 ),

 _onLoginButtonPressed() {
    _registerBloc
       .dispatch(RegisterButtonPressed(mobileNumber: mobileNumber.text));
  }

注册模型:

@JsonSerializable(nullable:false)
class ModelRegister {
  String mobile_number;

  ModelRegister({@required this.mobile_number});

  factory ModelRegister.fromJson(Map<String,dynamic> json)=>_$ModelRegisterFromJson(json);

  Map<String,dynamic> toJson()=>_$ModelRegisterToJson(this);
}

服务器请求数据:

'mobile_number' => 'required|string|unique:users|min:11|max:11',

RegisterEvent类:

abstract class RegisterEvent{}

class RegisterButtonPressed extends RegisterEvent{
  String mobileNumber;

  RegisterButtonPressed({@required this.mobileNumber});

  @override
  String toString()=>'register button pressed';
}

RegisterState类:

abstract class RegisterState {}

class RegisterInitial extends RegisterState {}

class RegisterSuccessful extends RegisterState {
  String message;
  RegisterSuccessful({@required this.message});
}

class RegisterVerificationCode extends RegisterState {}

class RegisterFailure extends RegisterState {
  String message;

  RegisterFailure({@required this.message});
}

class RegisterLoading extends RegisterState {}

RegisterBloc类:

class RegisterBloc extends Bloc<RegisterEvent, RegisterState> {
  RegisterRepository registerRepository;

  RegisterBloc({this.registerRepository});

  @override
  Stream<RegisterState> mapEventToState(RegisterEvent event) async* {
    if (event is RegisterButtonPressed) {
      yield RegisterLoading();

      final registerResponse =
          await registerRepository.register(mobileNumber: event.mobileNumber);

      try {
        if (registerResponse == null) {
          yield RegisterFailure(message: 'ERROR');
        } else {
          switch (registerResponse.status) {
            case 1: //wrong http verb
              yield RegisterFailure(message: Strings.httpVerbError);
              break;
        }
      } catch (_) {
        yield RegisterFailure(message: Strings.failToExecuteRegister);
      }
    }
  }

  @override
  RegisterState get initialState => RegisterInitial();
}

0 个答案:

没有答案