主键在打印时为null,并且检索结果,但是在打印时,ID仍为null

时间:2019-01-28 19:02:21

标签: flutter sqflite

用户ID为null,列为主键,因此不可能为null

我正在学习Flutter和SQFlite,我正在按照有关Udemey的课程中的示例进行操作,并在字母上键入它,但它对教师有效,对我却不起作用

我简化了所有内容,现在只有一个文件,ID为空

main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';

void main() async {
//  debugPrint("Hello World");
  var db = dbhelp();
  User user1 = User("User1");
  int insertResult = await db.saveUser(user1);
  debugPrint("insert result is " + insertResult.toString());
  User searchResult = await db.retrieveUser(insertResult);
  debugPrint(searchResult.toString());
}

class dbhelp {
  static final dbhelp _instance = dbhelp.internal();
  dbhelp.internal();
  factory dbhelp() => _instance;
  static Database _db;
  void _onCreate(Database _db, int newVersion) async {
    await _db.execute(
        "CREATE TABLE MYTABLE(ID INTEGER PRIMARY KEY, userName TEXT NOT NULL)");
  }

  Future<Database> initDB() async {
    Directory documentDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentDirectory.path, "appdb.db");
    Database newDB = await openDatabase(path, version: 1, onCreate: _onCreate);
    return newDB;
  }

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

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

  Future<User> retrieveUser(int id) async {
    var dbClient = await db;
    if (id == null) {
      print("The ID is null, cannot find user with Id null");
      var nullResult =
          await dbClient.rawQuery("SELECT * FROM MYTABLE WHERE ID is null");
      return User.fromMap(nullResult.first);
    }
    String sql = "SELECT * FROM MYTABLE WHERE ID = $id";
    var result = await dbClient.rawQuery(sql);
    if (result.length != 0) {
      return User.fromMap(result.first);
    } else {
      return null;
    }
  }
}

class User {
  String _userName;
  int _id;
  String get userName => _userName;
  int get id => _id;
  User(this._userName, [this._id]);
  User.map(dynamic obj) {
    this._userName = obj['userName'];
    this._id = obj['id'];
  }
  User.fromMap(Map<String, dynamic> map) {
    this._userName = map["userName"];
    if (map["id"] != null) {
      this._id = map["id"];
    } else {
      print("in fromMap, Id is null");
    }
  }
  Map<String, dynamic> toMap() {
    Map map = Map<String, dynamic>();
    map["userName"] = this._userName;
    if (_id != null) {
      map["id"] = _id;
    } else {
      print("in toMap, id is null");
    }
    return map;
  }

  @override
  String toString() {
    return "ID is ${this._id} , Username is ${this._userName} }";
  }
}

当我打印id时,它应该打印一个不为null的数字

但始终为空

1 个答案:

答案 0 :(得分:0)

我有些困惑。选择后的简单打印显示了该问题:

String sql = "SELECT * FROM MYTABLE WHERE ID = $id";
var result = await dbClient.rawQuery(sql);
print(result);

输出:

[{ID: 1, userName: User1}]

列名区分大小写。有时使用id(在toMap / fromMap中),有时使用ID(在创建表中)。确保在任何地方都使用相同的名称。