当布尔值在 Flutter 中为 true 和 false 时显示不同的小部件

问题描述

问题:在我实现 REST API 时,我遇到了一些小部件的条件显示。 所以 API 响应返回一个 bool 值。现在,如果 bool 值为 false,那么我想在 UI 中显示一个 TextField 小部件,否则只是一个文本小部件。 现在我对我应该如何实施它感到困惑。

API 的功能如下 我们会在他们的正文中发送一个带有电子邮件的帖子请求。 API 会检查 API 是否存在于他们的数据库中,作为回报,他们将发送真假和响应。

我将在此附上我的代码片段。

API 管理器

class ApiService {
  static const int TIME_CONST = 20;
  Future<UserCheck> apiUserCheck(dynamic param) async {
    var client = http.Client();

    try {
      var response = await client
          .post(
              Uri.https(
                  "baseUrl","endpoint"),body: param)
          .timeout(Duration(seconds: TIME_CONST));

      if (response.statusCode == 200) {
        print('Response Body: ${response.body}');
        final data = jsonDecode(response.body);
        return UserCheck(
          user: data['user_exists'],);
      } else {
        print("The response status is not 200");
        return param;
      }
    } on SocketException {
      throw FetchDataException('message','url');
    } on TimeoutException {
      throw ApiNotRespondingException("message","url");
    }
  }
}

class UserCheck {
  final bool? user;
  UserCheck({this.user});
}

UI 文件中使用的函数

        checkUserExist() {
        final check = ApiService();
        check.apiUserCheck({
          "email": emailController.text,}).then((value) {
          if (value.user == true) {
            print('User Exist: ');
          } else {
            print('User Does Not exists');
          }
        });
      }

用户界面部分:

 Padding(
                padding: EdgeInsets.fromLTRB(15,20,15,5),child: TextField(
                  obscureText: true,decoration: InputDecoration(
                    border: OutlineInputBorder(),labelText: 'Password',),////////IF ELSE BLOCK TO BE USED HERE TO DISPLAY COMDITIONAL WIDGETS/////

              SizedBox(
                height: 50.h,

注意:API 的实现效果很好,我可以验证电子邮件是否存在。我收到的响应正文为真假。

提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)