1

我在我的项目中使用 Scoped 模型插件,我不知道如何从 db Sqflite 中保存和获取表是否为空。请帮我。

这是数据库中的一些代码

addUser(UserModel user) async{
    final db = await database;
    var table = await db.rawQuery("SELECT MAX(id)+1 as id FROM User");
    int id = table.first["id"];
    //insert to the table using the new id
    var raw = await db.rawInsert(
        "INSERT Into Client (id,first_name,last_name,gender,username,email,is_email_verified,birthdate,phone,is_activated)"
            " VALUES (?,?,?,?,?,?,?,?,?,?)",
        [id, user.firstName, user.lastName, user.gender, user.email, user.isEmailVerified, user.birthdate, user.phone, user.isActivated]);
    return raw;
  }

并在 Scoped 模型中

UserModel userModel = UserModel(
  firstName: profileData.firstName,
  lastName: profileData.lastName,
  gender: profileData.gender,
  username: profileData.username,
  email: profileData.email,
  isEmailVerified: profileData.isEmailVerified,
  birthdate: profileData.birthDate,
  phone: profileData.phone,
  isActivated: profileData.isActivated,
);

print('usermodel ${userModel.toJson()}');
// here to save? 

和小部件类,

return Scaffold(
          appBar: AppBar(

            title: Center(
              child: ScopedModelDescendant<MainModel>(
                builder: (BuildContext context, Widget child, MainModel model){
                  return Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      model.isLoading ? Text('name surname', style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w500)) : Text('${model.profile.firstName} ${model.profile.lastName}', style: TextStyle(color: Colors.white,  fontSize: 18, fontWeight: FontWeight.w500),),
                      Text('Баланс \$0', style: TextStyle(color: Colors.white, fontSize: 15, fontWeight: FontWeight.w300),),
                    ],
                  );
                },
              ),
            ),
            elevation: 0,

我应该保存在 Scoped Model 还是 Widget 类中?我应该在小部件类中初始化并关闭吗?

4

0 回答 0