【问题标题】:FormatException: Unexpected character (at character 1) error while using email_auth packageFormatException:使用 email_auth 包时出现意外字符(在字符 1 处)错误
【发布时间】:2021-06-27 11:18:05
【问题描述】:

#你好!我正在使用 email_auth: ^0.0.1+4 Package 来确认用户电子邮件,它也工作了几天,我还注册了 2 3 个用户,但突然它开始显示这个 FormatException: Unexpected character (at character 1) 和 DOCTYPE html 错误。我已添加注册文件请检查并帮助。谢谢。#

注册页面

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:vanfly/common.dart';
import 'package:vanfly/providers/user_provider.dart';
import 'package:vanfly/screens/home.dart';
import 'package:vanfly/widgets/loading.dart';
import 'package:email_auth/email_auth.dart';

bool verified = false;

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

class _SignUpState extends State<SignUp> {
  final _formKey = GlobalKey<FormState>();
  final _key = GlobalKey<ScaffoldState>();

  TextEditingController _email = TextEditingController();
  TextEditingController _password = TextEditingController();
  TextEditingController _name = TextEditingController();
  TextEditingController otp = TextEditingController();
  bool hidePass = true;
  void sendOTP() async {
    EmailAuth.sessionName = 'Vanfly';
    var res = await EmailAuth.sendOtp(receiverMail: _email.value.text);
    if (res) {
      _key.currentState.showSnackBar(SnackBar(
          content: Text("An OTP has sent to your email please verify")));
    } else {
      _key.currentState
          .showSnackBar(SnackBar(content: Text('Something went wrong')));
    }
  }

  bool verifyOTP(){
    var res =  EmailAuth.validate(receiverMail: _email.value.text, userOTP: otp.value.text);
    if (res) {
      return verified = true;
    } else {
      _key.currentState
          .showSnackBar(SnackBar(content: Text('Something went wrong')));
      return verified = false;
    }
  }

  @override
  Widget build(BuildContext context) {
    final user = Provider.of<UserProvider>(context);

    return Scaffold(
      key: _key,
      body: user.status == Status.Authenticating
          ? Loading()
          : Stack(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(0),
                  child: Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(16),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.grey[350],
                          blurRadius:
                              20.0, // has the effect of softening the shadow
                        )
                      ],
                    ),
                    child: Form(
                        key: _formKey,
                        child: ListView(
                          children: <Widget>[
                            Padding(
                              padding: const EdgeInsets.all(16.0),
                              child: Container(
                                  alignment: Alignment.topCenter,
                                  child: Image(
                                    image: AssetImage(
                                        'images/vanfly Logo/vanfly_logo.png'),
                                    width: 300,
                                    height: 300,
                                  )),
                            ),
                            Padding(
                              padding: const EdgeInsets.fromLTRB(
                                  14.0, 8.0, 14.0, 8.0),
                              child: Material(
                                borderRadius: BorderRadius.circular(10.0),
                                color: Colors.grey.withOpacity(0.3),
                                elevation: 0.0,
                                child: Padding(
                                  padding: const EdgeInsets.only(left: 12.0),
                                  child: ListTile(
                                    title: TextFormField(
                                      controller: _name,
                                      decoration: InputDecoration(
                                          hintText: "Full name",
                                          icon: Icon(Icons.person_outline),
                                          border: InputBorder.none),
                                      validator: (value) {
                                        if (value.isEmpty) {
                                          return "The name field cannot be empty";
                                        }
                                        return null;
                                      },
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            Padding(
                              padding: const EdgeInsets.fromLTRB(
                                  14.0, 8.0, 14.0, 8.0),
                              child: Material(
                                borderRadius: BorderRadius.circular(10.0),
                                color: Colors.grey.withOpacity(0.2),
                                elevation: 0.0,
                                child: Padding(
                                  padding: const EdgeInsets.only(left: 12.0),
                                  child: ListTile(
                                    title: TextFormField(
                                      keyboardType: TextInputType.emailAddress,
                                      controller: _email,
                                      decoration: InputDecoration(
                                          hintText: "Email",
                                          suffixIcon: TextButton(
                                              onPressed: () {
                                                sendOTP();
                                              },
                                              child: Text('Verify')),
                                          icon: Icon(Icons.alternate_email),
                                          border: InputBorder.none),
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            Padding(
                              padding: const EdgeInsets.fromLTRB(
                                  14.0, 8.0, 14.0, 8.0),
                              child: Material(
                                borderRadius: BorderRadius.circular(10.0),
                                color: Colors.grey.withOpacity(0.2),
                                elevation: 0.0,
                                child: Padding(
                                  padding: const EdgeInsets.only(left: 12.0),
                                  child: ListTile(
                                    title: TextFormField(keyboardType: TextInputType.number,
                                      controller: otp,
                                      decoration: InputDecoration(
                                          hintText: "otp",
                                          suffixIcon: TextButton(
                                            onPressed: () {
                                              sendOTP();
                                            },
                                            child: Text('Resend'),
                                          ),
                                          icon: Icon(Icons.pin),
                                          border: InputBorder.none),
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            Padding(
                              padding: const EdgeInsets.fromLTRB(
                                  14.0, 8.0, 14.0, 8.0),
                              child: Material(
                                borderRadius: BorderRadius.circular(10.0),
                                color: Colors.grey.withOpacity(0.3),
                                elevation: 0.0,
                                child: Padding(
                                  padding: const EdgeInsets.only(left: 12.0),
                                  child: ListTile(
                                    title: TextFormField(
                                      controller: _password,
                                      obscureText: hidePass,
                                      decoration: InputDecoration(
                                          hintText: "Password",
                                          icon: Icon(Icons.lock_outline),
                                          border: InputBorder.none),
                                      validator: (value) {
                                        if (value.isEmpty) {
                                          return "The password field cannot be empty";
                                        } else if (value.length < 6) {
                                          return "the password has to be at least 6 characters long";
                                        }
                                        return null;
                                      },
                                    ),
                                    trailing: IconButton(
                                        icon: Icon(Icons.remove_red_eye),
                                        onPressed: () {
                                          setState(() {
                                            hidePass = !hidePass;
                                          });
                                        }),
                                  ),
                                ),
                              ),
                            ),
                            Padding(
                              padding: const EdgeInsets.fromLTRB(
                                  14.0, 8.0, 14.0, 8.0),
                              child: Material(
                                  borderRadius: BorderRadius.circular(20.0),
                                  color: Colors.pinkAccent,
                                  elevation: 0.0,
                                  child: MaterialButton(
                                    onPressed: () async {
                                      verifyOTP();

                                      if (verified == true &&
                                          _formKey.currentState.validate()) {
                                        if (!await user.signUp(_name.text,
                                            _email.text, _password.text)) {
                                          // ignore: deprecated_member_use
                                          _key.currentState.showSnackBar(
                                              SnackBar(
                                                  content:
                                                      Text("Sign up failed")));
                                          return;
                                        }
                                        changeScreenReplacement(
                                            context, MyHomePage());
                                      }
                                    },
                                    minWidth: MediaQuery.of(context).size.width,
                                    child: Text(
                                      "Sign up",
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                          color: Colors.white,
                                          fontWeight: FontWeight.bold,
                                          fontSize: 20.0),
                                    ),
                                  )),
                            ),
                            Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: InkWell(
                                    onTap: () {
                                      Navigator.pop(context);
                                    },
                                    child: Text(
                                      "I already have an account",
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                          color: Colors.black, fontSize: 16),
                                    ))),
                          ],
                        )),
                  ),
                ),
              ],
            ),
    );
  }
}

错误

I/flutter ( 5091): Email ID is valid
I/flutter ( 5091): --This error is from the package--
I/flutter ( 5091): FormatException: Unexpected character (at character 1)
I/flutter ( 5091): <!DOCTYPE html>
I/flutter ( 5091): ^
I/flutter ( 5091): --End package error message--

【问题讨论】:

  • 嗨@NitishGarg,包已经更新,请参考那里。如果您觉得有任何困难,请与我联系,或开始新的讨论或问题。我会从那里向前推进。谢谢
  • 这是错误是因为服务器已加载,现在通过最新更新,您可以设置自定义生产服务器并将其链接。

标签: firebase flutter dart authentication


【解决方案1】:

错误来自包本身。

用于发送 OTP 的包 calls 的 endpoint 已关闭,因此它返回错误页面而不是包预期的 JSON。这会导致parsing HTML 中出现错误,这就是错误显示的原因:

FormatException: Unexpected character (at character 1)
<!DOCTYPE html>
^

我建议您为此使用不同的服务,例如 firebase_auth 或在项目中使用 create an issue。

【讨论】:

  • 您好,先生!我希望你做得很好,实际上我已经在堆栈上发布了 2 -3 个查询,但没有人回复。我需要你的帮助,你是唯一帮助我解决我的问题的人,我是新手,请帮帮我。我会等待你的回应。谢谢。
猜你喜欢
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
  • 2022-11-07
  • 2021-07-31
  • 2021-03-30
  • 2019-08-17
  • 2022-07-31
  • 2020-03-07
相关资源
最近更新 更多