范围模型中的Sqflite

时间:2019-03-08 09:01:23

标签: flutter sqflite scoped-model

我在项目中使用了作用域模型插件,并且如果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;
  }

和作用域模型

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,

我应该保存在作用域模型还是Widget类中?我应该在小部件类中初始化并关闭吗?

0 个答案:

没有答案