簡體   English   中英

如何解決flutter中http錯誤代碼400缺少參數?

[英]How to solve http error code 400 missing parameters in flutter?

我有一個注冊頁面,我通過傳遞參數電子郵件、用戶名、密碼、名字和姓氏向服務器發出 http.post 請求。 我已經嘗試了一切,但無法解決問題。 每當我點擊注冊按鈕時,它都會顯示以下內容 - “I/flutter (2914): 400 I/flutter (2914): {"code":"rest_missing_callback_param","message":"Missing parameter(s): email , username, password","data":{"status":400,"params":["email","username","password"]}}"

我已經瀏覽了其他帖子,但他們沒有解決我的問題,所以請幫忙。 提前致謝。

import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:restaurant_app/globalVar.dart';
import 'package:restaurant_app/homescreen.dart';

class SignUp extends StatefulWidget {
 @override
 _SignUpState createState() => _SignUpState();
}

class _SignUpState extends State<SignUp> with SingleTickerProviderStateMixin 
 {
  TabController controller;
 TextEditingController _firstnameController;
 TextEditingController _lastnameController;
 TextEditingController _usernameController;
 TextEditingController _emailController;
 TextEditingController _passwordController;
 TextEditingController _rePasswordController;

 bool loading;

 final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState> 
 ();

 @override
 void initState() {
  // TODO: implement initState
  super.initState();
  controller = new TabController(length: 2, vsync: this);
  loading = false;
  _firstnameController = new TextEditingController(text: "Vishal");
  _lastnameController = new TextEditingController(text: "Das");
  _usernameController = new TextEditingController(text: "vishaldas");
_emailController = new TextEditingController(text: "vissudas@gmail.com");
_passwordController = new TextEditingController(text: "Vishal@123");
_rePasswordController = new TextEditingController(text: "Vishal@123");
}

@override
void dispose() {
// TODO: implement dispose
super.dispose();
controller.dispose();
setState(() {
  loading = false;
});

_emailController.dispose();
_usernameController.dispose();
_passwordController.dispose();
_firstnameController.dispose();
_lastnameController.dispose();
_rePasswordController.dispose();

}

  //  static final String fullnameKey = 'fullname';
  //  static final String emailKey = 'email';
  //  static final String passwordkey = 'password';

signUp(email, username, password, firstname, lastname) async {
setState(() {
  loading = true;
});

var body = json.encode({
  "email": email,
  "username": username,
  "password": password,
  "firstname": firstname,
  "lastname": lastname,
});

String basicAuth =
    'Basic' + base64Encode(utf8.encode('$username:$password'));

Map<String, String> headers = {
  HttpHeaders.AUTHORIZATION: basicAuth,
};

print(basicAuth);

await http
    .post("${GlobalVar.url}wp-json/wc/v2/customers",
        body: body, headers: headers)
    .then((response) {
  var body = json.decode(response.body);
  print(response.statusCode);
  print(response.body);
  if (response.statusCode == 200) {
    loading = false;
    Navigator.pushReplacement(context,
        MaterialPageRoute(builder: (BuildContext ctx) => HomePage()));
  } else {
    final snackBar = SnackBar(content: Text(body['message']));
    _scaffoldKey.currentState.showSnackBar(snackBar);
  }
  setState(() {
    loading = false;
  });
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
  resizeToAvoidBottomPadding: false,
  key: _scaffoldKey,
  body: Container(
    decoration: BoxDecoration(
      image: DecorationImage(
        image: AssetImage('images/art.png'),
        fit: BoxFit.fill,
        colorFilter: ColorFilter.mode(
            Colors.white12.withOpacity(0.2), BlendMode.dstATop),
      ),
    ),
    child: ListView(
      shrinkWrap: true,
      physics: BouncingScrollPhysics(),
      children: <Widget>[
        SizedBox(
          height: MediaQuery.of(context).size.height / 35,
        ),
        Align(
          alignment: Alignment.topCenter,
          child: CircleAvatar(
            backgroundColor: Colors.grey,
            radius: 55.0,
            backgroundImage: AssetImage('images/logo.png'),
          ),
        ),
        SizedBox(
          height: MediaQuery.of(context).size.height / 40,
        ),
        Stack(
          alignment: Alignment.center,
          children: <Widget>[
            SizedBox(
              height: 2.0,
              child: new Center(
                child: new Container(
                  height: 10.0,
                  color: Colors.black12,
                ),
              ),
            ),
            Row(
              children: <Widget>[
                SizedBox(
                  width: MediaQuery.of(context).size.width / 4,
                ),
                Chip(
                  label: Text(
                    "SIGN IN",
                    style: TextStyle(color: Colors.white, fontSize: 18.0),
                  ),
                  backgroundColor: Color(0xFFD1A155),
                ),
                SizedBox(
                  width: MediaQuery.of(context).size.width / 35,
                  child: Container(
                    width: MediaQuery.of(context).size.height / 12,
                    height: 2.0,
                    color: Colors.white,
                  ),
                ),
                Chip(
                  label: Text(
                    "SIGN UP",
                    style: TextStyle(color: Colors.white, fontSize: 18.0),
                  ),
                  backgroundColor: Colors.black87,
                ),
              ],
            )
          ],
        ),
        SizedBox(
          height: MediaQuery.of(context).size.height / 30,
        ),
        Align(
            alignment: Alignment.center,
            child: Text(
              "Sign Up Maa ki Rasoi",
              style: TextStyle(
                fontSize: 20.0,
                color: Color(0xFFD1A155),
              ),
            )),
        SizedBox(
          height: MediaQuery.of(context).size.height / 30,
        ),
        Column(
          children: <Widget>[
            Theme(
              data: ThemeData(
                hintColor: Colors.black26,
                primaryColor: Color(0xFFD1A155),
              ),
              child: Padding(
                padding: const EdgeInsets.only(left: 15.0, right: 15.0),
                child: TextField(
                  controller: _firstnameController,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(borderSide: BorderSide()),
                      prefixIcon: Icon(
                        Icons.person,
                        color: Color(0xFFD1A155),
                      ),
                      hintText: 'First name',
                      hintStyle: TextStyle(
                          color: Colors.black,
                          fontWeight: FontWeight.w400)),
                  keyboardType: TextInputType.text,
                ),
              ),
            ),
            SizedBox(
              height: MediaQuery.of(context).size.height / 50,
            ),
            Theme(
              data: ThemeData(
                hintColor: Colors.black26,
                primaryColor: Color(0xFFD1A155),
              ),
              child: Padding(
                padding: const EdgeInsets.only(left: 15.0, right: 15.0),
                child: TextField(
                  controller: _lastnameController,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(borderSide: BorderSide()),
                      prefixIcon: Icon(
                        Icons.person,
                        color: Color(0xFFD1A155),
                      ),
                      hintText: 'Last name',
                      hintStyle: TextStyle(
                          color: Colors.black,
                          fontWeight: FontWeight.w400)),
                  keyboardType: TextInputType.text,
                ),
              ),
            ),
            SizedBox(
              height: MediaQuery.of(context).size.height / 50,
            ),
            Theme(
              data: ThemeData(
                  primaryColor: Color(0xFFD1A155),
                  hintColor: Colors.black26),
              child: Padding(
                padding: const EdgeInsets.only(left: 15.0, right: 15.0),
                child: TextField(
                  controller: _emailController,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(borderSide: BorderSide()),
                      prefixIcon: Icon(
                        Icons.email,
                        color: Color(0xFFD1A155),
                      ),
                      hintText: 'Email Address',
                      hintStyle: TextStyle(
                          color: Colors.black,
                          fontWeight: FontWeight.w400)),
                  keyboardType: TextInputType.emailAddress,
                ),
              ),
            ),
            SizedBox(
              height: MediaQuery.of(context).size.height / 50,
            ),
            Theme(
              data: ThemeData(
                hintColor: Colors.black26,
                primaryColor: Color(0xFFD1A155),
              ),
              child: Padding(
                padding: const EdgeInsets.only(left: 15.0, right: 15.0),
                child: TextField(
                  controller: _usernameController,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(borderSide: BorderSide()),
                      prefixIcon: Icon(
                        Icons.supervised_user_circle,
                        color: Color(0xFFD1A155),
                      ),
                      hintText: 'Username',
                      hintStyle: TextStyle(
                          color: Colors.black,
                          fontWeight: FontWeight.w400)),
                  keyboardType: TextInputType.text,
                ),
              ),
            ),
            SizedBox(
              height: MediaQuery.of(context).size.height / 50,
            ),
            Theme(
              data: ThemeData(
                hintColor: Colors.black26,
                primaryColor: Color(0xFFD1A155),
              ),
              child: Padding(
                padding: const EdgeInsets.only(left: 15.0, right: 15.0),
                child: TextField(
                  controller: _passwordController,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(borderSide: BorderSide()),
                      prefixIcon: Icon(
                        Icons.lock,
                        color: Color(0xFFD1A155),
                      ),
                      hintText: 'Password',
                      hintStyle: TextStyle(
                          color: Colors.black,
                          fontWeight: FontWeight.w400)),
                  keyboardType: TextInputType.text,
                  obscureText: true,
                ),
              ),
            ),
            SizedBox(
              height: MediaQuery.of(context).size.height / 50,
            ),
            Theme(
              data: ThemeData(
                  primaryColor: Color(0xFFD1A155),
                  hintColor: Colors.black26),
              child: Padding(
                padding: const EdgeInsets.only(left: 15.0, right: 15.0),
                child: TextField(
                  controller: _rePasswordController,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(borderSide: BorderSide()),
                      prefixIcon: Icon(
                        Icons.lock,
                        color: Color(0xFFD1A155),
                      ),
                      hintText: 'Re - Password',
                      hintStyle: TextStyle(
                          color: Colors.black,
                          fontWeight: FontWeight.w400)),
                  keyboardType: TextInputType.text,
                  obscureText: true,
                ),
              ),
            ),
            SizedBox(
              height: MediaQuery.of(context).size.height / 50,
            ),
            Padding(
              padding: const EdgeInsets.only(left: 15.0, right: 15.0),
              child: InkWell(
                onTap: () {
                  print(_emailController.text);
                  print(_passwordController.text);
                  print(_usernameController.text);
                  signUp(
                    _emailController.text,
                    _usernameController.text,
                    _passwordController.text,
                    _firstnameController.text,
                    _lastnameController.text,
                  );
                },
                child: loading
                    ? CircularProgressIndicator()
                    : Container(
                        height: MediaQuery.of(context).size.height / 13,
                        //width: MediaQuery.of(context).size.height / 1.8,
                        decoration: BoxDecoration(
                          color: Color(0xFFD1A155),
                          borderRadius: BorderRadius.circular(5.0),
                        ),
                        child: Center(
                          child: Text(
                            "SIGN UP",
                            style: TextStyle(
                                color: Colors.white, fontSize: 18.0),
                          ),
                        ),
                      ),
              ),
            )
          ],
        ),
        SizedBox(
          height: MediaQuery.of(context).size.height / 30,
        ),
        Stack(
          alignment: Alignment.center,
          children: <Widget>[
            SizedBox(
              height: 2.0,
              child: new Center(
                child: new Container(
                  height: 10.0,
                  color: Colors.black12,
                ),
              ),
            ),
            Container(
              height: MediaQuery.of(context).size.height / 18,
              width: MediaQuery.of(context).size.height / 11,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(23.0),
                  color: Colors.white,
                  border: Border.all(color: Colors.black12)),
              child: Center(
                  child: Text(
                "OR",
                style: TextStyle(fontSize: 18.0),
              )),
            ),
          ],
        ),
        SizedBox(
          height: MediaQuery.of(context).size.height / 40,
        ),
        Padding(
          padding: const EdgeInsets.only(left: 15.0, right: 15.0),
          child: Row(
            children: <Widget>[
              Container(
                height: MediaQuery.of(context).size.height / 13,
                width: MediaQuery.of(context).size.width / 2.2,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(5.0),
                    color: Colors.white,
                    border: Border.all(color: Colors.black12)),
                child: Row(
                  children: <Widget>[
                    SizedBox(
                      width: 18.0,
                    ),
                    Icon(Icons.tag_faces),
                    SizedBox(
                      width: 10.0,
                    ),
                    Text(
                      "Facebook",
                      style: TextStyle(fontSize: 22.0, color: Colors.blue),
                    ),
                  ],
                ),
              ),
              SizedBox(
                width: MediaQuery.of(context).size.width / 40,
              ),
              Container(
                height: MediaQuery.of(context).size.height / 13,
                width: MediaQuery.of(context).size.width / 2.3,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(5.0),
                    color: Colors.white,
                    border: Border.all(color: Colors.black12)),
                child: Row(
                  children: <Widget>[
                    SizedBox(
                      width: 18.0,
                    ),
                    Icon(Icons.tag_faces),
                    SizedBox(
                      width: 10.0,
                    ),
                    Text(
                      "Google+",
                      style: TextStyle(fontSize: 22.0, color: Colors.red),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
        SizedBox(
          height: MediaQuery.of(context).size.height / 30,
        ),
        Align(
          alignment: Alignment.center,
          child: InkWell(
            onTap: () => Navigator.pop(context),
            child: RichText(
                text: TextSpan(
                    text: "Already have an account?",
                    style: TextStyle(fontSize: 20.0, color: Colors.black87),
                    children: <TextSpan>[
                  TextSpan(
                      text: ' Log in',
                      style: TextStyle(
                          color: Color(0xFFD1A155),
                          fontWeight: FontWeight.bold)),
                ])),
          ),
        ),
        SizedBox(
          height: MediaQuery.of(context).size.height / 40,
        ),
       ],
      ),
    ),
  );
 }
}

錯誤代碼 400 通常由於服務器無法處理的錯誤請求(即意外參數、格式錯誤的語法)而發生。 如果評論中提到的內容是正確的,並且這里正在使用 woocommerce,那么我無法測試您的設置。 但是我可以建議的是按照本指南在客戶端中測試您的配置。 Postman是我可以建議測試 HTTP 請求的常用工具。 如果 API 與 Postman 配合良好,但請求在 Flutter 中仍然存在問題,我們可以從那里開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM