我是一名初级程序员,最近刚开始学习Flutter ,我无法解决这个问题.

import 'package:firebase_auth/firebase_auth.dart';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:todo_n_f_t_app/app/data/database.dart';
import 'package:todo_n_f_t_app/app/data/user.dart';
import 'package:todo_n_f_t_app/app/data/user_controller.dart';

import 'app/modules/login/views/login_view.dart';
import 'constants/firebase_constants.dart';

class AuthController extends GetxController {
static AuthController instance = Get.find();
late Rx<User?> _user;

@override
void onReady() {
super.onReady();
_user = Rx<User?>(auth.currentUser);
_user.bindStream(auth.userChanges());
ever(_user, _initialScreen);
}

_initialScreen(User? user) {
if (user == null) {
Get.offAll(() => LoginView());
} else {
Get.offAll(() => LoginView());
}
}

void register(String email, String password, String name) async {
try {
UserCredential _authResult = await auth.createUserWithEmailAndPassword(
email: email, password: password);

      UserModel _user = UserModel(
        id: _authResult.user?.uid,
        name: name,
        email: _authResult.user?.email,
      );
      if (await Database().createNewUser(_user)) {
        Get.find<UserController>().user = _user;
        Get.back();
      }
    } on FirebaseAuthException catch (e) {
  
      print(e.message);
      // Get.snackbar("Error", e.message!);
      Get.snackbar(
        "Error",
        e.message!,
        snackPosition: SnackPosition.BOTTOM,
      );
    } catch (e) {
      
      print(e.toString());
    }

}

void login(String email, password) async {
try {
await auth.signInWithEmailAndPassword(email: email, password: password);
} catch (e) {
Get.snackbar("About User", "User message",
snackPosition: SnackPosition.BOTTOM,
titleText: Text("Acount creation failed"),
messageText:
Text(e.toString(), style: TextStyle(color: Colors.white)));
}
}

void signOut() {
try {
auth.signOut();
} catch (e) {
print(e.toString());
}
}
}

下面是派生类:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:todo_n_f_t_app/auth_controller.dart';

class RegisterView extends GetView<AuthController> {
final TextEditingController _passwordTextController = TextEditingController();
final TextEditingController _emailTextController = TextEditingController();
final TextEditingController _usernameTextController = TextEditingController();

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: Container(
child: SafeArea(
left: false,
right: false,
child: Column(children: [
SizedBox(height: 40.0),
Container(
child: Container(
height: 100,
width: 100,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15.0),
gradient: SweepGradient(colors: [
Color(0xff998629),
Color(0xDE1C44D2),
Color(0xff582D95),
Color(0xffB10A3B),
Color(0xff269B27),
]),
),
child: Container(
height: 90,
width: 90,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15.0),
color: Color(0xB3000000),
),
child: Icon(Icons.done_outline,
color: Colors.white, size: 80.0),
),
)),
SizedBox(
height: 10,
),
Expanded(
child: Text(
'TodoNFT',
style: TextStyle(fontSize: 36.0),
)),
SizedBox(
width: MediaQuery.of(context).size.width,
height: 4,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(colors: [
Color(0xff1C44D2),
Color(0xff582D95),
Color(0xffB10A3B),
Color(0xff998629),
Color(0xff269B27),
])),
),
),
SizedBox(
height: 60.0,
),
Container(
width: MediaQuery.of(context).size.width - 20.0,
child: TextField(
controller: _emailTextController,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
),
filled: true,
fillColor: Color(0x33EBC4C4),
hintText: 'Email',
hintStyle: TextStyle(fontSize: 24.0, color: Colors.white),
suffix: Icon(Icons.mail_outline,
color: Colors.white, size: 24.0),
),
),
),
SizedBox(height: 60),
Container(
width: MediaQuery.of(context).size.width - 20.0,
child: TextField(
controller: _passwordTextController,
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
),
filled: true,
fillColor: Color(0x33EBC4C4),
hintText: 'Password',
hintStyle: TextStyle(fontSize: 24.0, color: Colors.white),
suffix: Icon(Icons.lock, color: Colors.white, size: 24.0),
),
),
),
SizedBox(height: 60),
Container(
width: MediaQuery.of(context).size.width - 20.0,
child: TextField(
controller: _usernameTextController,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
),
filled: true,
fillColor: Color(0x33EBC4C4),
hintText: 'Username',
hintStyle: TextStyle(fontSize: 24.0, color: Colors.white),
suffix: Icon(Icons.mail_outline,
color: Colors.white, size: 24.0),
),
),
),
SizedBox(height: 40),
SizedBox(
width: MediaQuery.of(context).size.width - 40.0,
height: 4,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(colors: [
Color(0xff1C44D2),
Color(0xff582D95),
Color(0xffB10A3B),
Color(0xff998629),
Color(0xff269B27),
])),
),
),
SizedBox(height: 30.0),
TextButton(
child: Container(
child: Text(
'Register',
style: TextStyle(fontSize: 36.0, color: Colors.white),
),
),
 onPressed: () {
 controller.register(
 _emailTextController.text,
 _passwordTextController.text,
 _usernameTextController.text);
 },
),
SizedBox(height: 30.0),
]),
),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/Background.png"),
fit: BoxFit.cover,
),
)));
}
}

如何从AuthController调用register方法.我在RegisterView中使用了加粗的onPressed方法,我在编辑器中没有任何错误,但当我想要注册时,代码会停止.它也不会在firestore集合中写入用户uid.

我try 将用户注册到FireStore,并获得了经过身份验证的用户,但我没有从经过身份验证的用户那里获得uid的新集合.

推荐答案

Here is the solution, just put a parent class into a variable and from that variable call a method

Flutter相关问答推荐

更新到Flutter 3.22,卡片和导航栏背景中没有显示 colored颜色

来自另一个屏幕的数据

ListTile未正确显示在flutter中

将目标平台设置为Flutter 构建

Flutter 布局-全屏幕容器

从外部类动态更改IconButton的图标

遇到图像路径格式问题

将图标呈现在Flutter 克牌小工具的角落上

从超级应用程序启动时,Flutter 小应用程序中未加载资源

'Flutter的无效常数值错误

从图标启动器打开时,VSCode未检测到Web设备

SliverAppbar 呈蓝色

如何给按钮的边框半径?

如何更改 DropDownMenu 的显示方向?

Flutter Dart 功能弹出错误的小部件?

在向 Google Route API 发送 HTTP Post 请求期间发生错误,

如何使用 Rrect 或类似工具在 flutter 中绘制梯形线?

如何从 Firebase `get()` 获取 `DocumentReference`

我的主屏幕上的 FutureBuilder 不断重新加载(但仅在其他屏幕上时)?

Flutter 上Denim blue colored颜色 的 colored颜色 矩阵代码是什么?