【问题标题】:sqflite + Flutter : map((e) => .... returns [Instance of Class]sqflite + Flutter : map((e) => .... 返回 [类实例]
【发布时间】:2021-02-01 04:48:48
【问题描述】:

我目前正在将 Sqflite 与 Flutter 一起用于正在开发的小型应用程序。

我的用户模型类如下:

class User {
  static const tblUser = 'users';
  static const colUserId = 'id';
  static const colUserName = 'name';
  static const colUserMail = 'mail';
  static const colUserPhone = 'phone';

  User({this.id, this.name, this.mail, this.phone});

  int id;
  String name;
  String mail;
  String phone;

  User.fromMap(Map<String, dynamic> map) {
    this.id = map[colUserId];
    this.name = map[colUserName];
    this.mail = map[colUserMail];
    this.phone = map[colUserPhone];
  }

  Map<String, dynamic> toMap() {
    var map = <String, dynamic>{
      colUserName: name,
      colUserMail: mail,
      colUserPhone: phone
    };
    if (id != null) {
      map[colUserId] = id;
    }
    return map;
  }
}

在 database_helper 类中,我创建了可以正常工作的数据库和表:

class DatabaseHelper {
  static const _databaseName = 'FinmanDatabase.db';
  static const _databaseVersion = 1;

  DatabaseHelper._();

  static final DatabaseHelper instance = DatabaseHelper._();

  Database _database;

  Future<Database> get database async {
    if (_database != null) return _database;
    _database = await _initDatabase();
    return _database;
  }

  _initDatabase() async {
    Directory dbDirectory = await getApplicationDocumentsDirectory();
    String dbpath = join(dbDirectory.path, _databaseName);
    return await openDatabase(dbpath,
        version: _databaseVersion, onCreate: _onCreateDB);
  }

  Future _onCreateDB(Database db, int version) async {
    await db.execute(''' 
    CREATE TABLE ${User.tblUser} (
      ${User.colUserId} INTEGER PRIMARY KEY AUTOINCREMENT,
      ${User.colUserName} TEXT NOT NULL,
      ${User.colUserMail} TEXT NOT NULL,
      ${User.colUserPhone} TEXT NOT NULL
    )
    ''');
  }}

但是,当我尝试获取用户进行颤动时,程序返回“用户实例”而不是内容。获取用户函数如下:

  Future<List<User>> fetchAllUser() async {
    Database db = await database;
    List<Map> userslist = await db.query(User.tblUser);
    int count = userslist.length;
    print('Printing $userslist from DBHelper + Length: $count');
    return userslist.length == 0 ? [] : userslist.map((e) => User.fromMap(e)).toList();
  }

这就是我在 Flutter 中的称呼:

class _HomeState extends State<Home> {
  User user = User();
  List<User> _users;
  String _dbExists;
  DatabaseHelper _dbhelper;
  int count = 0;

  @override
  void initState() {
    super.initState();
    setState(() {
      _dbhelper = DatabaseHelper.instance;
    });
    _refreshUserList();
  }

  _refreshUserList() async {
    List<User> x = await _dbhelper.fetchAllUser();
    setState(() {
      _users = x;
      print(_users);
      count = _users.length;
      print(count);
      if (count > 0) {
        _dbExists = 'Database exists';
      } else {
        _dbExists = 'Create a new Database';
      }
    });
  }}

虽然 print(count) 返回 1(即用户表中的记录数),但 print(_users) 返回 [Instance of User]??

感谢任何帮助。

【问题讨论】:

    标签: json list flutter sqflite


    【解决方案1】:

    当我尝试获取用户颤动时,程序返回 “用户实例”而不是内容

    因为 x 是List&lt;User&gt;

    要获取内容,可以使用for-loop

    for(var i in x){
       print(i.name); // it will print out the name
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      相关资源
      最近更新 更多