The getter 'user' isn't defined for the type 'SignUppage'. Try importing the library that defines 'user',

Viewed 209

hi I have stateful class and I want to call the usercredential that appears in Stream I want to call it in the main.dart but it appear to me that it is undefined anyone can help me please

this is the stateful class

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:insight_software/models/user.dart';
import 'package:insight_software/widgets/input_file.dart';

class SignUppage extends StatefulWidget {
  SignUppage({key}) : super(key: key);

  @override
  State<SignUppage> createState() => _SignUppageState();
}

class _SignUppageState extends State<SignUppage> {
  FirebaseAuth auth = FirebaseAuth.instance;
  String? myemail = '';
  String? mypassword = '';
  String? myconPassword = '';

  GlobalKey<FormState> formstate = new GlobalKey<FormState>();

  UserModel? _userFromFirebaseUser(User userCredential) {
    return userCredential != null ? UserModel(id: userCredential.uid) : null;
  }

  Stream<UserModel?> get userCredential {
    return auth
        .authStateChanges()
        .map((userCredential) => _userFromFirebaseUser(userCredential!));
  }

  Future SignUp() async {
    var formdata = formstate.currentState;

    if (formdata!.validate()) {
      print("valid");
      formdata.save();
      try {
        UserCredential userCredential =
            await auth.createUserWithEmailAndPassword(
                email: myemail!, password: mypassword!);
        return userCredential;
      } on FirebaseAuthException catch (e) {
        if (e.code == 'weak-password') {
          Navigator.of(context).pop();
          print('The password provided is too weak.');
        } else if (e.code == 'email-already-in-use') {
          Navigator.of(context).pop();
          print('The account already exists for that email.');
        }
      } catch (e) {
        print(e);
      }
    } else {}
  }

and this is the main.dart

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:insight_software/models/user.dart';
import 'package:provider/provider.dart';
import 'Screens/intro_screen.dart';
import 'Screens/login_screen1.dart';
import 'Screens/main_screens.dart';
import 'Screens/sign_up_screen.dart';
import 'Screens/sign_up_screen2.dart';
import 'package:insight_software/Screens/sign_up_screen.dart';


Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
 
 
  // This widget is the root of your application.
  final Future<FirebaseApp> _initialization = Firebase.initializeApp();
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _initialization,
      builder: (context, snapshot) {
        // Check for errors
        if (snapshot.hasError) {
          //return SomethingWentWrong();
        }
        // Once complete, show your application
        if (snapshot.connectionState == ConnectionState.done) {
          return StreamProvider<UserModel?>.value(
            value: SignUppage.userCredential,
            initialData: ,
            child: MaterialApp(
              debugShowCheckedModeBanner: false,
              title: 'Insight application',
              theme: ThemeData(
                primaryColor: Colors.blue,
                primarySwatch: Colors.lightBlue,
                backgroundColor: Colors.white,
                dividerColor: Colors.black,
                visualDensity: VisualDensity.adaptivePlatformDensity,
                textTheme: TextTheme(
                  headline1: TextStyle(
                      fontSize: 30,
                      color: Colors.white,
                      fontWeight: FontWeight.bold),
                  headline2: TextStyle(
                      fontSize: 22,
                      color: Colors.black.withAlpha(160),
                      fontWeight: FontWeight.bold),
                  headline3: TextStyle(
                      fontSize: 20, color: Colors.white.withAlpha(140)),
                  headline4: TextStyle(fontSize: 16),
                ),
                inputDecorationTheme: InputDecorationTheme(
                  // fill the buttons with colors
                  filled: true,
                  fillColor: Color(0xfff2f9fe),

                  // three modes for the input buttons email and password
                  enabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey.shade800),
                    borderRadius: BorderRadius.circular(25),
                  ),
                  disabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey.shade800),
                    borderRadius: BorderRadius.circular(25),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey.shade800),
                    borderRadius: BorderRadius.circular(25),
                  ),
                ),
              ),

              home: introScreen(),
              //connect the inrtoScreen with login screen
              routes: {
                // go from the iintro screen to the login screen
                'login': (context) => loginScreen(),
                'sign up': (context) => SignUppage(),
                'continue': (context) => SignUppage2(),
                'sign up new account': (context) => MainScreen(),
              },
            ),
          );
        };
        return Text("Loading");
      },
    );
  }
}

it is appears to me undefined in this line in the main

value: SignUppage.userCredential,

please help me quickly any one can know the answer please

1 Answers

There are 2 reasons why you can't call userCredential.

Number 1 is that userCredential is not a variable inside SignupPage. It's a variable inside _SignupPageState. Those two are different. In order to use this variable, you need to move it to the other class. You also have to move the auth variable as you need that in order to create the stream.

If you, by any chance want to use the userCredential inside the state class, you should call it using the widget variable, like this: widget.userCredential.

The second problem is that you are trying to access it from outside the class as if it was a static variable, your variable is not static, but I can't seem to find any reason why it shouldn't be, so you should add the static keyword:

static FirebaseAuth auth = FirebaseAuth.instance;
static Stream<UserModel?> get userCredential { ... }

Now I said earlier that you should use widget.userCredential if you ever want to access it from inside the state class, this is no longer true, now you should do this everywhere SignupPage.userCredential.

Final note, I think you should consider moving all this auth related code to a class of it's own and have the signup page call it, but this is not necesary.

Related