Skip to content
Back

Appwrite Login Issue login does not work

  • 0
  • Self Hosted
  • Flutter
  • Auth
Ewyn
11 Mar, 2025, 11:28

Hey there! im following this tutorial https://devdactic.com/flutter-authentication-appwrite on creating a login and register screen.

ive tried to do the exact same, but for some reason my app either crashes when logging in or gives me the following errors:

flutter: Error: LateInitializationError: Field 'account' has not been initialized. flutter: Error: Creation of a session is prohibited when a session is active. flutter: Initialization complete flutter: value: AuthStatus.unauthenticated

I will provide you with the code shortly.

TL;DR
Developers are experiencing crashes or errors related to a login issue with Appwrite. The errors include "Unhandled Exception: type 'Null' is not a subtype of type 'bool'" and "LateInitializationError: Field 'account' has not been initialized." The issue occurs when logging in or attempting to create a session. To address this, ensure the 'account' field is properly initialized in the 'AppWriteAuth' class. Additionally, check for any active sessions before creating a new one.
Ewyn
11 Mar, 2025, 11:50
TypeScript
import 'dart:async';
import 'package:appwrite/models.dart';
import 'package:flutter/foundation.dart';
import 'package:appwrite/appwrite.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:servicescheinapp/constants/constants.dart';

enum AuthStatus {
  uninitialized,
  authenticated,
  unauthenticated,
}

// Helper class for the appwrite database
// connects to Database and Storage
class AppWriteAuth extends ChangeNotifier {
  Client client = Client();
  late final Account account;
  late User _currentUser;

  AuthStatus _status = AuthStatus.uninitialized;

  // Getter methods
  User get currentUser => _currentUser;
  AuthStatus get status => _status;
  String? get username => _currentUser?.name;
  String? get email => _currentUser?.email;
  String? get userid => _currentUser?.$id;

  AppWriteAuth() {
    init();
    loadUser();
  }

  init() async {
    await dotenv.load(fileName: "constants.env");
    String serverURL = dotenv.env['SERVER_URL']!;
    String apiEndpoint = 'v1';
    client
        .setEndpoint('$serverURL$apiEndpoint')
        .setProject(appwriteProjectID2)
        .setSelfSigned(status: true);
// For self signed certificates, only use for development
    account = Account(client);
    print('Initialization complete');
  }

  loadUser() async {
    try {
      final user = await account.get();
      _status = AuthStatus.authenticated;
      _currentUser = user;
    } catch (e) {
      _status = AuthStatus.unauthenticated;
    } finally {
      notifyListeners();
    }
  }

  Future<User> createUser(
      {required String email, required String password}) async {
    try {
      final user = await account.create(
          userId: ID.unique(), email: email, password: password);
      return user;
    } finally {
      notifyListeners();
    }
  }

  Future<Session> createEmailSession(
      {required String email, required String password}) async {
    try {
      final session = await account.createEmailPasswordSession(
          email: email, password: password);
      _currentUser = await account.get();
      _status = AuthStatus.authenticated;
      return session;
    } finally {
      notifyListeners();
    }
  }

  // check Authentication state
  Future<void> checkAuth() async {
    try {
      _currentUser = await account.get();
      // Logged in
      print('Logged in and authenticated');
      print('User: ${_currentUser.email}');
    } catch (e) {
      // Not logged in
      print('Not logged in');
      print('Error: $e');
    }
  }

  signOut() async {
    try {
      await account.deleteSession(sessionId: 'current');
      _status = AuthStatus.unauthenticated;
    } finally {
      notifyListeners();
    }
  }
}

This is my auth file

Ewyn
11 Mar, 2025, 11:51

This is my LOGIN page.

i use the register and password forgot button to test signout/ if user is logged in.

Ewyn
11 Mar, 2025, 11:52

current behaviour is:

either crashes with correct login info or gives me these errors: [ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: type 'Null' is not a subtype of type 'bool' #0 new Target.fromMap (package:appwrite/src/models/target.dart:54:19) #1 new User.fromMap.<anonymous closure> (package:appwrite/src/models/user.dart:103:67) #2 MappedListIterable.elementAt (dart:_internal/iterable.dart:442:31) #3 ListIterator.moveNext (dart:_internal/iterable.dart:371:26) #4 new List.from (dart:core-patch/array_patch.dart:30:17) #5 new User.fromMap (package:appwrite/src/models/user.dart:103:16) #6 Account.get (package:appwrite/services/account.dart:23:24) <asynchronous suspension> #7 AppWriteAuth.createEmailSession (package:servicescheinapp/backend/appwrite/appwrite_auth.dart:76:22) <asynchronous suspension> #8 _AppwriteLoginState.signIn (package:servicescheinapp/backend/appwrite/appwrite_login.dart:37:7) <asynchronous suspension> flutter: value: AuthStatus.unauthenticated

checking account with getAUTH gives me flutter: Error: LateInitializationError: Field 'account' has not been initialized.

I also use the Provider package as in the tutorial

Ewyn
11 Mar, 2025, 11:54

Appwrite Login Issue login does not work

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more