使用sqflite数据库退出Flutter应用

时间:2019-07-10 06:41:21

标签: flutter dart

我正在尝试编写用于注销Flutter应用程序登录的代码(地图数据库由sqflite创建)。

但是我收到以下错误消息:

  

flutter:NoSuchMethodError:方法'notify'在null上调用。       接收者:null       尝试调用:notify('AuthState'的实例)

在下面提供了所需文件的代码。

我正在尝试将注销功能合并到home_screen.dart文件中,但是我感觉缺少auth.dartauth_provider.dartlogin_screen.dart和{{1}之间的链接}文件。该问题所需文件的代码如下:

文件: database_helper.dart

home_screen.dart

文件: auth.dart

import 'dart:async';
import 'dart:io' as io;

import 'package:path/path.dart';
import 'package:better_login/user.dart';
import 'package:sqflite/sqflite.dart' ;
import 'package:path_provider/path_provider.dart';

class DatabaseHelper {
  static final DatabaseHelper _instance = new
  DatabaseHelper.internal();
  factory DatabaseHelper() => _instance;

  static Database _db;

  Future<Database> get db async {
    if(_db != null)
      return _db;
     _db = await initDb();
    return _db;
  }

  DatabaseHelper.internal();

  initDb() async {
    io.Directory documentsDirectory = await
    getApplicationDocumentsDirectory();
    String path = join(documentsDirectory.path, "main.db");
    var theDb = await openDatabase(path, version: 1, onCreate:
    _onCreate);
    return theDb;
  }


  void _onCreate(Database db, int version) async {
    // When creating the db, create the table
    await db.execute(
        "CREATE TABLE User(username TEXT,password TEXT)");
    print("Created tables");
  }

   Future<int> saveUser(User user) async {
    var dbClient = await db;
    int res = await dbClient.insert("User", user.toMap());
    return res;
  }

  Future<int> deleteUsers() async {
    var dbClient = await db;
    int res = await dbClient.delete("User");
    return res;
  }

  Future<bool> isLoggedIn() async {
    var dbClient = await db;
    var res = await dbClient.query("User");
    return res.length > 0? true: false;
  }
}

文件: auth_provider.dart

import 'package:better_login/database_helper.dart';

enum AuthState{ LOGGED_IN, LOGGED_OUT }

abstract class AuthStateListener {
  void onAuthStateChanged(AuthState state);

}

class AuthStateProvider {
  static final AuthStateProvider _instance = new
  AuthStateProvider.internal();

  List<AuthStateListener> _subscribers;

  factory AuthStateProvider() => _instance;
  AuthStateProvider.internal() {
    _subscribers = new List<AuthStateListener>();
    initState();
  }

   void initState() async {
    var db = new DatabaseHelper();
    var isLoggedIn = await db.isLoggedIn();
    if(isLoggedIn)
      notify(AuthState.LOGGED_IN);
    else
      notify(AuthState.LOGGED_OUT);
  }

  void subscribe(AuthStateListener listener) {
    _subscribers.add(listener);
  }

  void dispose(AuthStateListener listener) {
    for(var l in _subscribers) {
      if(l == listener)
        _subscribers.remove(l);
    }
  }

   void notify(AuthState state) {
    _subscribers.forEach((AuthStateListener s) =>
        s.onAuthStateChanged(state));
  }
}

文件: home_screen.dart

import 'package:flutter/material.dart';
import 'package:better_login/auth.dart';

class AuthProvider extends InheritedWidget {
  const AuthProvider({Key key, Widget child, this.auth}) :    super(key: key, child: child);
  final AuthStateListener auth;

  @override
  bool updateShouldNotify(InheritedWidget oldWidget) => true;

   static AuthProvider of(BuildContext context) {
    return context.inheritFromWidgetOfExactType(AuthProvider);
  }
}

0 个答案:

没有答案