Flutter,将文本字段插入sqflite

l5tcr1uw  于 2023-11-21  发布在  Flutter
关注(0)|答案(2)|浏览(145)

我尝试使用textfields填充sqflite表,但它创建了一个单独的表,而不是填充各个字段

class _InputBarState extends State<InputBar> {
  final _controller = TextEditingController();
  final _controller1 = TextEditingController();
  final _controller2 = TextEditingController();
  final _focusNode = FocusNode();

  String get text => _controller.text.trim();
  String get qnt => _controller1.text.trim();
  String get um => _controller2.text.trim();

字符串
...

TextField(
controller: _controller,
),

TextField(
controller: _controller1,
),

TextField(
controller: _controller2,
),

IconButton(
                  color: primaryColor,
                  padding: const EdgeInsets.only(right: 10),
                  icon: const Icon(Icons.add),
                  onPressed: text.isEmpty || um.isEmpty || qnt.isEmpty  ? null : () => _onSubmitted(text, um, qnt),
                ),


...

void _onSubmitted(String text, String qnt, String um) {
    if (text.isEmpty || qnt.isEmpty || um.isEmpty) return;

    widget.onSubmitted(text);
    widget.onSubmitted(qnt);
    widget.onSubmitted(um);
    _controller.clear();
    _controller1.clear();
    _controller2.clear();
    _focusNode.requestFocus();
  }


如果我填写的3个领域:1,2,3,我有作为输出的附加图像..而不是它应该是一个单一的行一样:1 2 3

Text(item.name + item.qnt + item.um ),


帮助解决这个问题

fcg9iug3

fcg9iug31#

您提供的代码不足以澄清问题。如果您能提供widget.onSubmitted(text)回调的代码片段,那就太好了。
但是假设你想把textqntum的值存储在一行中,你必须把回调函数改为下面这样,而不是用text、qnt、um单独调用它:

widget.onSubmitted(text: text, qnt: qnt, um: um)

字符串

xmjla07d

xmjla07d2#

你的修改我得到这个错误
immage

Future<String> createItem(String listId, String name, String qnt, String um) async {
final id = uuid();
final maxPosition = (await _crdt.query('''
  SELECT max(position) AS max_position FROM todos
  WHERE list_id = ? AND is_deleted = 0
''', [listId])).first['max_position'] as int? ?? -1;
await _crdt.execute('''
  INSERT INTO todos (id, list_id, name, qnt, um, done, position, creator_id, created_at)
  VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)
''', [
  id,
  listId,
  name,
  qnt,
  um,
  0,
  maxPosition + 1,
  userId,
  DateTime.now().toUtcString,
]);
return id;

字符串
}

相关问题