Home > database >  How to access in flutter method in derived class from parent class?
How to access in flutter method in derived class from parent class?

Time:04-06

I'm beginner programmer and started recently with flutter and I cant solve this problem. Here is my parent class :

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());
}
}
}

Here is derived class:

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,
),
)));
}
}

How can I invoke register method from AuthController. I bolded onPressed method in RegisterView, I don't have any errors in editor but code brakes when I want to register. Also it doesn't write user uid in firestore collection.

I tried to register user into FireStore and I get authenticated user but I don't get new collection with uid from user I get in authenticated.

CodePudding user response:

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

CodePudding user response:

koji si ti meni kralj!! guglaj to bree

  • Related