当我尝试使用 API 和 getx 进行注册时,出现此错误,我还使用共享首选项来制作启动画面

问题描述 投票:0回答:2

我目前正在开发一个用于用户登录和注册的 Flutter 应用程序,并且我正在使用 GetX 状态管理库和 API 来实现此目的。但是,我遇到了错误,我正在寻求帮助来解决它。

[Get] the improper use of a GetX has been detected.
      You should only use GetX or Obx for the specific widget that will be updated.
      If you are seeing this error, you probably did not insert any observable variables into GetX/Obx
      or insert them outside the scope that GetX considers suitable for an update
      (example: GetX => HeavyWidget => variableObservable).
      If you need to update a parent widget and a child widget, wrap each one in an Obx/GetX.

这是我的注册页面

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'controller.dart';

class RegisterPage extends StatelessWidget {
  final controller = Get.put(RegisterPageController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Register Page"),
      ),
      body: Obx(() => ListView(
            padding: EdgeInsets.all(20),
            children: [
              TextFormField(
                controller: controller.cUsername,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  hintText: "Masukkan Username anda",
                  labelText: "Username",
                ),
              ),
              SizedBox(height: 20),
              TextFormField(
                controller: controller.cPass,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  hintText: "Masukkan Password",
                  labelText: "Password",
                ),
              ),
              SizedBox(height: 20),
              TextFormField(
                controller: controller.cFullname,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  hintText: "Masukkan Nama Lengkap anda",
                  labelText: "Nama Lengkap",
                ),
              ),
              SizedBox(height: 20),
              TextFormField(
                controller: controller.cEmail,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  hintText: "Masukkan Email anda",
                  labelText: "Email",
                ),
              ),
              SizedBox(height: 20),
              ElevatedButton(
                onPressed: () {
                  controller.registerUser();
                },
                child: Text("Register"),
              ),
            ],
          )),
    );
  }
}

这是我在控制器页面中的代码

class RegisterPageController extends GetxController {
  TextEditingController? cUsername;
  TextEditingController? cPass;
  TextEditingController? cFullname;
  TextEditingController? cEmail;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    cUsername = new TextEditingController();
    cPass = new TextEditingController();
    cFullname = new TextEditingController();
    cEmail = new TextEditingController();
  }

  registerUser() async {
    final baseUrl = 'https://mediadwi.com/api/latihan/register-user';
    final response = await http.post(Uri.parse(baseUrl), body: {
      'username': cUsername?.text,
      'password': cPass?.text,
      'full_name': cFullname?.text,
      'email': cEmail?.text
    });
    try {
      if (response.statusCode == 200) {
        Get.to(() => LoginPage());
        Get.snackbar("Selamat", "Pembuatan akun anda Sukses, ");
      } else {
        Get.snackbar('Maaf', 'Akun anda gagal dibuat');
      }
    } catch (e) {
      print(e);
    }
  }
}

这是我的启动画面https://pastebin.com/YXNpum7e
在我的主要部分,我连接到了我的闪屏

flutter sharedpreferences flutter-getx
2个回答
0
投票

您只需删除

obx
小部件即可。

希望这对您有帮助。


0
投票

删除列表视图上的 Obx。 注意:如果您使用像 .obs 这样的反应变量,那么您需要从 obx 包装小部件

© www.soinside.com 2019 - 2024. All rights reserved.