#flutter #dart
#флаттер #dart
Вопрос:
имя метода — createResgister, так что это метод, который возвращает будущее, содержащее ответ. хорошо, и в нем есть TextEdittingController каждой формы, которую я хочу опубликовать, и API, который я тоже хочу опубликовать
хорошо, у меня ошибка при попытке опубликовать форму регистрации в коде apiKey:
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:logistics_app/models/registerApi.dart';
import 'package:logistics_app/ui/dashboard_view.dart';
import 'package:logistics_app/utils/validation.dart';
import 'package:http/http.dart' as http;
import '../ui/login_view.dart';
class UserOnboardingModel extends ChangeNotifier with ValidationMixin{
TextEditingController firstController = TextEditingController();
TextEditingController lastController = TextEditingController();
TextEditingController passwordController = TextEditingController();
TextEditingController emailController = TextEditingController();
TextEditingController phoneController = TextEditingController();
bool showPassword = true;
final formKey = GlobalKey<FormState>();
Future<RegisterModel> _futureRegister;
Future<RegisterModel> createRegister()async{
final http.Response response = await http.post(
"apiKey",
body: jsonEncode(<String, String>{
'firstName' : validateFirstName(firstController.text),
'lastName' : validateLastName(lastController.text),
'emailAddress' : validateEmail(emailController.text),
'phoneNumber' : validatePhoneNumber(phoneController.text),
'password' : validatePassword(passwordController.text)
})
);
if(response.statusCode == 201){
return RegisterModel.fromJson(json.decode(response.body));
}else{
throw Exception('Failed to create Register');
}
}
void passRegister(){
_futureRegister = createRegister();
}
void registerForm(){
FutureBuilder<RegisterModel>(
future: _futureRegister,
builder: (context, snapshot){
if(snapshot.hasData){
return Column(
children: [
Text(snapshot.data.firstName),
Text(snapshot.data.lastName),
Text(snapshot.data.emailAddress),
Text(snapshot.data.phoneNumber),
Text(snapshot.data.password)
],
);
}else if(snapshot.hasError){
return Text("${snapshot.error}");
}
return CircularProgressIndicator();
},
);
}
void togglePassword(){
showPassword = !showPassword;
notifyListeners();
}
void validateForm(){
formKey.currentState.validate();
}
void moveToLogin(context){
//go to login page
if(formKey.currentState.validate()){
Navigator.pushNamed(context, LogIn.LOG_IN_ROUTE);
}
else{
Fluttertoast.showToast(
msg: "Please Fill The Form",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 5,
backgroundColor: Colors.black,
);
}
}
void moveToDashBoardView(context){
//go to login page
if(formKey.currentState.validate()){
Navigator.push(context, MaterialPageRoute(
builder: (context) => DashboardView()
));
}
else{
Fluttertoast.showToast(
msg: "Please Fill The Form",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 5,
backgroundColor: Colors.black,
);
}
}
void moveToSignup(){
//go to sign up page
notifyListeners();
}
void moveToForgottenPassword(){
//go to forgotten password page
notifyListeners();
}
}
теперь я получаю сообщение об ошибке: Failed to create Register
E/flutter ( 7901): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: Exception: Failed to create Register
E/flutter ( 7901): #0 UserOnboardingModel.createRegister (package:logistics_app/models/userOnboardingModel.dart:42:7)
E/flutter ( 7901): <asynchronous suspension>
E/flutter ( 7901): #1 UserOnboardingModel.passRegister (package:logistics_app/models/userOnboardingModel.dart:47:23)
E/flutter ( 7901): #2 _buildCircleAvatar.<anonymous closure>.<anonymous closure> (package:logistics_app/ui/sign_up_view.dart:241:18)
E/flutter ( 7901): #3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:992:19)
E/flutter ( 7901): #4 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1098:38)
E/flutter ( 7901): #5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:184:24)
E/flutter ( 7901): #6 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:524:11)
E/flutter ( 7901): #7 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:284:5)
E/flutter ( 7901): #8 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:256:7)
E/flutter ( 7901): #9 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:158:27)
E/flutter ( 7901): #10 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:224:20)
E/flutter ( 7901): #11 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:200:22)
E/flutter ( 7901): #12 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:158:7)
E/flutter ( 7901): #13 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:104:7)
E/flutter ( 7901): #14 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:88:7)
E/flutter ( 7901): #15 _rootRunUnary (dart:async/zone.dart:1206:13)
E/flutter ( 7901): #16 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 7901): #17 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter ( 7901): #18 _invoke1 (dart:ui/hooks.dart:267:10)
E/flutter ( 7901): #19 _dispatchPointerDataPacket (dart:ui/hooks.dart:176:5)
E/flutter ( 7901):
Комментарии:
1. хорошо … теперь, учитывая, что вы сами выдаете эту ошибку в своем коде, в чем именно заключается ваш вопрос?
2. должно ли быть наоборот?
3. что-то вроде этого:
if(response.statusCode == 201){ throw Exception('Failed to create Register'); ); }else{ return RegisterModel.fromJson(json.decode(response.body) }
4. Что вы имеете в виду под «подразумевается ли это», это ваш код , если вы не знаете, что это должно быть, мы, конечно, не сможем помочь.
5. @nvoigt я хочу, чтобы он опубликовал введенную форму