selfteaching/How-To-Ask-Questions-The-Smart-Way

error: The expression doesn't evaluate to a function, so it can't be invoked. (invocation_of_non_function_expression at [tuned_in] lib/resources/firebase_methods.dart:17)

goc20 opened this issue · 1 comments

goc20 commented

//this my firebase_methods.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:tuned_in/models/user.dart';
import 'package:tuned_in/utils/utilities.dart';

class FirebaseMethods {
final FirebaseAuth _auth = FirebaseAuth.instance;
GoogleSignIn _googleSignIn = GoogleSignIn();
static final Firestore firestore = Firestore.instance;

//user class
User user = User();

Future getCurrentUser() async {
FirebaseUser currentUser;
currentUser = await _auth.currentUser();
return currentUser;
}

Future signIn() async {
GoogleSignInAccount _signInAccount = await _googleSignIn.signIn();
GoogleSignInAuthentication _signInAuthentication =
await _signInAccount.authentication;

final AuthCredential credential = GoogleAuthProvider.getCredential(
    accessToken: _signInAuthentication.accessToken,
    idToken: _signInAuthentication.idToken);

FirebaseUser user = await _auth.signInWithCredential(credential);
return user;

}

Future authenticateUser(FirebaseUser user) async {
QuerySnapshot result = await firestore
.collection("users")
.where("email", isEqualTo: user.email)
.getDocuments();

final List<DocumentSnapshot> docs = result.documents;

//if user is registered then length of list > 0 or else less than 0
return docs.length == 0 ? true : false;

}

Future addDataToDb(FirebaseUser currentUser) async {
String username = Utils.getUsername(currentUser.email);

user = User(
    uid: currentUser.uid,
    email: currentUser.email,
    name: currentUser.displayName,
    profilePhoto: currentUser.photoUrl,
    username: username);

firestore
    .collection("users")
    .document(currentUser.uid)
    .setData(user.toMap(user));

}

Future signOut() async {
await _googleSignIn.disconnect();
await _googleSignIn.signOut();
return await _auth.signOut();
}

}

//this is user.dart file
class User {
String uid;
String name;
String email;
String username;
String status;
int state;
String profilePhoto;

User({
this.uid,
this.name,
this.email,
this.username,
this.status,
this.state,
this.profilePhoto,
});

Map toMap(User user) {
var data = Map<String, dynamic>();
data['uid'] = user.uid;
data['name'] = user.name;
data['email'] = user.email;
data['username'] = user.username;
data["status"] = user.status;
data["state"] = user.state;
data["profile_photo"] = user.profilePhoto;
return data;
}

User.fromMap(Map<String, dynamic> mapData) {
this.uid = mapData['uid'];
this.name = mapData['name'];
this.email = mapData['email'];
this.username = mapData['username'];
this.status = mapData['status'];
this.state = mapData['state'];
this.profilePhoto = mapData['profile_photo'];
}
}

//this is login_screen.dart
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:tuned_in/resources/firebase_repository.dart';
import 'package:shimmer/shimmer.dart';
import 'package:tuned_in/utils/universal_variables.dart';
import 'home_screen.dart';

class LoginScreen extends StatefulWidget {
@OverRide
LoginScreenState createState() => LoginScreenState();
}

class LoginScreenState extends State {
FirebaseRepository _repository = FirebaseRepository();

bool isLoginPressed = false;

@OverRide
Widget build(BuildContext context) {
_repository.signOut();
return Scaffold(
backgroundColor: UniversalVariables.blackColor,
body: Stack(
children: [
Center(
child: loginButton(),
),
isLoginPressed
? Center(
child: CircularProgressIndicator(),
)
: Container()
],
),
);
}

Widget loginButton() {
return Shimmer.fromColors(
baseColor: Colors.white,
highlightColor: UniversalVariables.senderColor,
child: FlatButton(
padding: EdgeInsets.all(35),
child: Text(
"LOGIN",
style: TextStyle(
fontSize: 35, fontWeight: FontWeight.w900, letterSpacing: 1.2),
),
onPressed: () => performLogin(),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
),
);
}

void performLogin() {
print("tring to perform login");

setState(() {
  isLoginPressed = true;
});

_repository.signIn().then((FirebaseUser user) {
  print("something");
  if (user != null) {
    authenticateUser(user);
  } else {
    print("There was an error");
  }
});

}

void authenticateUser(FirebaseUser user) {
_repository.authenticateUser(user).then((isNewUser) {
setState(() {
isLoginPressed = false;
});

  if (isNewUser) {
    _repository.addDataToDb(user).then((value) {
      Navigator.pushReplacement(context,
          MaterialPageRoute(builder: (context) {
        return HomeScreen();
      }));
    });
  } else {
    Navigator.pushReplacement(context,
        MaterialPageRoute(builder: (context) {
      return HomeScreen();
    }));
  }
});

}
}

goc20 commented

I forgot this
//this is my main.dart file
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:tuned_in/resources/firebase_repository.dart';
import 'package:tuned_in/screens/home_screen.dart';
import 'package:tuned_in/screens/login_screen.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
@OverRide
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State {
FirebaseRepository _repository = FirebaseRepository();

@OverRide
Widget build(BuildContext context) {
return MaterialApp(
title: "tuned_in",
debugShowCheckedModeBanner: false,
home: FutureBuilder(
future: _repository.getCurrentUser(),
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return HomeScreen();
} else {
return LoginScreen();
}
},
),
);
}
}