flutter - 如何在 Dart 中对 Textfield onChange 进行去抖动?

我正在尝试开发一个 TextField,它可以在 Firestore 数据库中的数据发生更改时更新它们。它似乎有效,但我需要防止 onChange 事件多次触发。

在 JS 中我会使用 lodash _debounce() 但在 Dart 中我不知道该怎么做。我读过一些去抖动库,但我不知道它们是如何工作的。

这是我的代码,它只是一个测试,所以可能有些奇怪:

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';


class ClientePage extends StatefulWidget {

  String idCliente;


  ClientePage(this.idCliente);

  @override
  _ClientePageState createState() => new _ClientePageState();

  
}

class _ClientePageState extends State<ClientePage> {

  TextEditingController nomeTextController = new TextEditingController();


  void initState() {
    super.initState();

    // Start listening to changes 
    nomeTextController.addListener(((){
        _updateNomeCliente(); // <- Prevent this function from run multiple times
    }));
  }


  _updateNomeCliente = (){

    print("Aggiorno nome cliente");
    Firestore.instance.collection('clienti').document(widget.idCliente).setData( {
      "nome" : nomeTextController.text
    }, merge: true);

  }



  @override
  Widget build(BuildContext context) {

    return new StreamBuilder<DocumentSnapshot>(
      stream: Firestore.instance.collection('clienti').document(widget.idCliente).snapshots(),
      builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
        if (!snapshot.hasData) return new Text('Loading...');

        nomeTextController.text = snapshot.data['nome'];


        return new DefaultTabController(
          length: 3,
          child: new Scaffold(
            body: new TabBarView(
              children: <Widget>[
                new Column(
                  children: <Widget>[
                    new Padding(
                      padding: new EdgeInsets.symmetric(
                        vertical : 20.00
                      ),
                      child: new Container(
                        child: new Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[
                            new Text(snapshot.data['cognome']),
                            new Text(snapshot.data['ragionesociale']),
                          ],
                        ),
                      ),
                    ),
                    new Expanded(
                      child: new Container(
                        decoration: new BoxDecoration(
                          borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(20.00),
                            topRight: Radius.circular(20.00)
                          ),
                          color: Colors.brown,
                        ),
                        child: new ListView(
                          children: <Widget>[
                            new ListTile(
                              title: new TextField(
                                style: new TextStyle(
                                  color: Colors.white70
                                ),
                                controller: nomeTextController,
                                decoration: new InputDecoration(labelText: "Nome")
                              ),
                            )
                          ]
                        )
                      ),
                    )
                  ],
                ),
                new Text("La seconda pagina"),
                new Text("La terza pagina"),
              ]
            ),
            appBar: new AppBar(
              title: Text(snapshot.data['nome'] + ' oh ' + snapshot.data['cognome']),
              bottom: new TabBar(          
                tabs: <Widget>[
                  new Tab(text: "Informazioni"),  // 1st Tab
                  new Tab(text: "Schede cliente"), // 2nd Tab
                  new Tab(text: "Altro"), // 3rd Tab
                ],
              ),
            ),
          )
        );
        
      },
    );

    print("Il widget id è");
    print(widget.idCliente);
    
  }
}

最佳答案

实现

导入依赖:

import 'dart:async';

在您的小部件状态中声明一个计时器:

Timer? _debounce;

添加监听方法:

_onSearchChanged(String query) {
    if (_debounce?.isActive ?? false) _debounce.cancel();
    _debounce = Timer(const Duration(milliseconds: 500), () {
        // do something with query
    });
    }

别忘了清理:

@override
void dispose() {
    _debounce?.cancel();
    super.dispose();
}

用法

在你的构建树 Hook onChanged 事件:

child: TextField(
        onChanged: _onSearchChanged,
        // ...
    )

https://stackoverflow.com/questions/51791501/

相关文章:

android-studio - 使用 Android Studio 自动在右括号后插入注释

dart - 在所有页面 Flutter 中持久化 AppBar Drawer

flutter - 如何在 Flutter 中创建带有圆角的模态底页?

flutter - 没有 ScrollView 的刷新指示器

dart - 如何在 flutter 的另一个有状态小部件中访问在一个有状态小部件中创建的对象

flutter - 如何在 flutter 中听焦点变化?

dart - Flutter:使用按钮更改标签栏 View 中的当前标签

flutter - 如何让应用程序保持清醒?

flutter - 在 Flutter 中使用 SafeArea

dart - Flutter:在简历上更新小部件?