0

I am trying to get the val of a var in a different file in flutter, I tried to do something like this : New x = New(); 'New is my class', x.variable = ...

This didn't work for me. Any ideas how I can make something like this work?

import 'auth/LoginPage.dart' as login;

class WelcomePage extends StatefulWidget {
  @override
  _WelcomePageState createState() => _WelcomePageState();
}

class _WelcomePageState extends State<WelcomePage> {
  final PageController _pageController = PageController();
  var x = login.userType;
dKen
  • 3,078
  • 1
  • 28
  • 37
omar developer
  • 189
  • 1
  • 3
  • 13

3 Answers3

0

So you need to create a file login.dart from where you want to import methods like this:

//login.dart

enum UserType { Firebase, Google, Twitter }

getUserTypeFirebase() {
  return UserType.Firebase;
}

getUserTypeGoogle() {
  return UserType.Google;
}

getUserTypeTwitter() {
  return UserType.Twitter;
}

Then you create the library login_lib.dart using the library statement and export statement to export other files or packages that you want to use from this library. In this case we export the file login.dart like described bellow.

//login_lib.dart

library login;

export 'login.dart';

And after that you can import the login_lib in the class you want like this:

//welcome_page.dart

import 'package:flutter/material.dart';
import 'login_lib.dart' as login;

class WelcomePage extends StatelessWidget {
  var x = login.getUserTypeGoogle();

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
JRamos29
  • 880
  • 7
  • 20
  • Hey Is it possible to use shared preferences in this ? – omar developer Jun 26 '20 at 08:15
  • After you added some code in the question, i think that maybe you need to add an `export` statement in the class from where you can get some variable/method. I believe the discussions in these posts can be what you need: https://stackoverflow.com/questions/44480039/dart-need-explanation-of-library-part-and-import-export and https://stackoverflow.com/questions/27763378/when-to-use-part-part-of-versus-import-export-in-dart – JRamos29 Jun 26 '20 at 16:47
  • But how can I use export here because I have no experience in it :( – omar developer Jun 27 '20 at 06:46
  • I don't know exactly you want to do. Depending on what you are trying to achieve, maybe there's another ways, that can be pretty simple to implement than that. But I edited my answer. Take a look to see if solves your problem. – JRamos29 Jun 27 '20 at 12:20
0

I figured out how to do autologin but when I tried it it gave an error,Is there sth wrong? CODE :

    @override
  void initState() {
    super.initState();
    FirebaseAuth.instance.currentUser().then(
      (res) async {
        print(userType);
        if (res != null && userType == 'Admin') {
          Navigator.pushReplacementNamed(context, '/AdminPage');
        }
        if (res != null && userType == 'Student') {
          Navigator.pushReplacementNamed(context, '/StudentPage');
        }
        if (res != null && userType == 'Teacher') {
          Navigator.pushReplacementNamed(context, '/TeacherPage');
        }

It says userType is null, so My new question is about the capability of using a var outside of initState inside it ? Confused Right ?

omar developer
  • 189
  • 1
  • 3
  • 13
0

The file i want the value from :

importme.dart

  • Remove the _ from the extended part...
  • Make the variable static ..

class MultiSelectChip extends StatefulWidget { final List reportList; final Function(List) onSelectionChanged;

_MultiSelectChip(this.reportList, {required this.onSelectionChanged});

@override **_**MultiSelectChipState createState() => MultiSelectChipState(); }

class _MultiSelectChipState extends State {
static List selectedChoices = [];

mainfile.dart

import 'package:ytsubmeapp/widgets/multichoice.dart';

List x = MultiSelectChipState.selectedChoices;

niyamxsept
  • 105
  • 6